Android 实现开机启动 Service 或 APP
- 创建一个广播接收器,重写onReceive()方法,在其中启动想要开机启动的Service或者App
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: 2016/12/21 启动特定服务或者启动某个应用
Log.e("Lionet", "onReceive");
Intent startApp = context.getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(startApp);
}
}
- 修改配置文件,在
<intent-fliter>
中加配置
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</receiver>
- 添加权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
注:系统设置里面需要允许该应用开机启动,否则无效