第一步:在AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
第二步:新建Receiver类
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent intent = new Intent(context, MainActivity.class); intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } }
第三步:注册MyReceiver
<receiver android:name=".MyReceiver" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>