Android开机自启动

1.定义一个BroadcastReceiver

Java代码复制代码
  1. publicclassBootReceiverextendsBroadcastReceiver{
  2. publicvoidonReceive(Contextctx,Intentintent){
  3. Log.d("BootReceiver","systembootcompleted");
  4. //startactivity
  5. Stringaction="android.intent.action.MAIN";
  6. Stringcategory="android.intent.category.LAUNCHER";
  7. Intentmyi=newIntent(ctx,CustomDialog.class);
  8. myi.setAction(action);
  9. myi.addCategory(category);
  10. myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  11. ctx.startActivity(myi);
  12. //startservice
  13. Intents=newIntent(ctx,MyService.class);
  14. ctx.startService(s);
  15. }
  16. }
public class BootReceiver extends BroadcastReceiver {public void onReceive(Context ctx, Intent intent) {Log.d("BootReceiver", "system boot completed");//start activityString action="android.intent.action.MAIN";String category="android.intent.category.LAUNCHER";Intent myi=new Intent(ctx,CustomDialog.class);myi.setAction(action);myi.addCategory(category);myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);ctx.startActivity(myi);//start serviceIntent s=new Intent(ctx,MyService.class);ctx.startService(s);}}

2.配置Receiver的许可,允许接收系统启动消息,在AndroidManifest.xml中:

Xml代码复制代码
  1. <uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

3.配置Receiver,可以接收系统启动消息,在AndroidManifest.xml中

Xml代码复制代码
  1. <receiverandroid:name=".app.BootReceiver">
  2. <intent-filter>
  3. <actionandroid:name="android.intent.action.BOOT_COMPLETED"/>
  4. <categoryandroid:name="android.intent.category.HOME"/>
  5. </intent-filter>
  6. </receiver>
        <receiver android:name=".app.BootReceiver">        <intent-filter>        <action android:name="android.intent.action.BOOT_COMPLETED"/>                <category android:name="android.intent.category.HOME" />        </intent-filter>        </receiver>

4.启动模拟器,可以看到系统启动后,弹出一个对话框。

更多相关文章

  1. Android 推送消息(MQTT) PHP发送
  2. Anko:Android 代码动态布局的新方案
  3. Android 获取系统权限的代码
  4. 编译代码报出Android library projects cannot be launched错误
  5. 解决Eclipse3.6中Android 代码自动补全卡死的问题
  6. 按键消息传播流程(WindowManagerService.java)
  7. android 下测试网络的丢包率和延迟 的代码

随机推荐

  1. Android中实现全屏、无标题栏的两种办法(
  2. android中listview的一些样式设置,自定义
  3. 关于Android的自动化测试,你需要了解的5个
  4. 【入门篇】Android学习笔记——常用布局
  5. Android(安卓)5.1 - 7.1 系统(framework)
  6. [转]Android都支持那些Java特性
  7. android为HttpClient和HttpURLConnection
  8. 2020京东最新Android面试真题解析【建议
  9. Android(安卓)圆角边框实现
  10. Android(安卓)属性动画ValueAnimator和Ob