我正在尝试在设备重新启动时自动启动Android服务。我试图使用Receive_Boot_Complete权限,使用Boot_Complete意图操作的BroadcastReceiver完成此操作但没有成功。我非常清楚Android 3.0应用程序在重启后处于停止状态,因此没有接收器能够运行。但是,有几个移动安全应用程序(如Lookout)在重新启动时运行服务和进程。他们如何才能做到这一点?
<!-- Listen for the device starting up -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="com.tpss.beacon.BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
public class BootCompleteReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, UpdateGeofenceService.class));
}
}
答案 0 :(得分:0)
系统启动时,会发送BOOT_COMPLETED意图的广播。因此,您必须创建一个BroadcastReceiver来捕获该意图:
public class BReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, final Intent intent)
{
Log.i(TAG, "onReveive BOOT_COMPLETED");
// start your service
context.startService(new Intent("your_service"));
}
}
然后修改清单:
<receiver android:name=".BReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
不要忘记添加权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />