android开机自动启动APP(用BroadcastReceive实现)
1、新建一个继承BroadcastReceive的类
public class StartBoot extends BroadcastReceiver { public StartBoot() { } @Override public void onReceive(Context context, Intent intent) { Intent intent2 = new Intent(context,MainActivity.class);// intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// context.startActivity(intent2); } }
2.修改AndroidManifest.xml
<receiver android:name=".StartBoot"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED"/> <category android:name="android.intent.category.HOME"/> </intent-filter> </receiver>
3、添加权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>