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锁屏控制
  2. android 中的 handler
  3. AES Crypto 在 Android(安卓)P(Android(安卓)9.0) 被删除 解决方案
  4. Android中的动画
  5. 高通平台Android(安卓)N SystemUI添加qcnvitems qcrilhook
  6. 使用Android(安卓)studio导入源码工程
  7. android如何改变默认横竖屏方向
  8. android底部中间凸出导航 BottomProtruding
  9. android-使App全屏 - 随心

随机推荐

  1. Android沉浸式状态栏(包含 小米、魅族)
  2. android 读取本地超大图片
  3. 安卓开发之android使用webview时按后退会
  4. Android的listview、gridview 笔记
  5. Cordova 3.x 源码分析(2) -- cordova.js概
  6. Android开发中遇到的小问题 一
  7. android: 制作system.img(yaffs2) 镜像
  8. android singleTask几个注意点
  9. android中Pull解析器
  10. android修改Zxing源码,避免出现错误信息