在 AndroidManifest.xml application 的节点下

<receiver
    android:name=".BootBroadcastReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>
1
2
3
4
5
6
7
8
9
10

新建 BootBroadcastReceiver.java

/**
 * 自动重启
 */
public class BootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent newIntent = new Intent(context, MainActivity.class);  // 要启动的Activity
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(newIntent);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13