定义一个BroadcastReceiver
- publicclassBootReceiverextendsBroadcastReceiver{
- publicvoidonReceive(Contextctx,Intentintent){
- Log.d("BootReceiver","systembootcompleted");
- //startactivity
- Stringaction="android.intent.action.MAIN";
- Stringcategory="android.intent.category.LAUNCHER";
- Intentmyi=newIntent(ctx,CustomDialog.class);
- myi.setAction(action);
- myi.addCategory(category);
- myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- ctx.startActivity(myi);
- //startservice
- Intents=newIntent(ctx,MyService.class);
- ctx.startService(s);
- }
- }
- <uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
3.配置Receiver,可以接收系统启动消息,在AndroidManifest.xml中
- <receiverandroid:name=".app.BootReceiver">
- <intent-filter>
- <actionandroid:name="android.intent.action.BOOT_COMPLETED"/>
- <categoryandroid:name="android.intent.category.HOME"/>
- </intent-filter>
- </receiver>