如何使用广播接收器运行服务



如何使用广播接收器启动后台服务。 为相机锁定操作做了一个服务,但一段时间后功能不起作用。 服务正在安卓设备中运行。 但是 onstart命令中的服务方法不起作用。

用过action.user_present但它不起作用.

 public class camerareceiver extends BroadcastReceiver{ 
public static String TESTACT_S = "android.intent.action.USER_PRESENT"; 
     @Override
     public void onReceive(Context context, Intent intent) {
     if(intent.getAction().equals(TESTACT_S))
      { context.startService(newIntent("com.simsys.camera.ServiceTemplate")); } }

BroadcastReceiver 启动服务:

public class CamReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
   Toast.makeText(context, "ACTION_USER_PRESENT",  Toast.LENGTH_LONG).show();
 context.startService(new Intent(context,ServiceTemplate.class));
}
}
}

在清单中:

<receiver android:name= ".CamReceiver">
           <intent-filter>
             <action android:name="android.intent.action.USER_PRESENT"/>
           </intent-filter>
        </receiver>

最新更新