APP开机自启动
通过创建BootBroadcastReceiver类继承自BroadcastReceiver,重写onReceive方法接收ACTION_BOOT_COMPLETED的广播
1、启动Activity
Intent bootIntent = new Intent(context, MainActivity.class);
bootIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //此处必加,不然会报错
context.startActivity(bootIntent);
2、启动Service
Intent bootIntent = new Intent(context, UploadImageService.class);
bootIntent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) ; //为了避免被强制停止后接收不到广播
context.startService(bootIntent);
3、启动整个应用
Intent bootIntent = context.getPackageManager().getLaunchIntentForPackage(包名); //包名为要唤醒的应用包名
context.startActivity(bootIntent);
必做步骤
① 在AndroidMainfest.xml文件中添加要启动的BroadCastReceiver
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
② 添加开机启动权限