方法:
监听开机广播,在BroadcastReceiver中自启APP;
具体实现:
1,在项目的清单文件AndroidManifest中配置receiver标签,在intent-filter标签中配置要接受的action 标签,开机广播action 的name属性值为android.intent.action.BOOT_COMPLETED;
//接收开机广播
<receiver
android:name=".BootStartApp"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
2,在清单文件中声明权限
//接收系统开机广播
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
3,创建广播接收者 BootBroadcastReceiver
public class BootStartApp extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
String action = intent.getAction().toString();
Logger.i("开机广播");
if (action.equals(Intent.ACTION_BOOT_COMPLETED))
//启动APP某个Activity即可,或者主activity,也可是其他组件等
context.startActivity(new Intent(context, MainActivity.class));
}
}
4,成功接收开机广播的条件是:APP需要用户手动点击启动过一次才可以;
其他相关
接收不到开机广播的问题:
https://blog.csdn.net/baidu_27196493/article/details/78269674