1. Android Manifest file to add self-starting permission: <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
2. Define a receiver
Manifest文件注册receiver <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver>
Java code:
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){ Intent newIntent = context.getPackageManager() .getLaunchIntentForPackage("com.example.demo"); //apk包名 context.startActivity(newIntent); } } }
3. The self-starting App should be built-in as much as possible, and the reason why it cannot be self-started
(1) The device does not allow self-starting, such as mobile phones of various brands
(2) The self-starting App is installed on the SD card