Android App startup

In recent projects, the function of startup and self startup is useful. Here is a summary for everyone to learn and discuss!

Actual combat drill

Test mobile phone: Hongmi mobile phone Redmi 6A Android version 9
Huawei mobile phone DUA-AL00 Android version 8.1.0
Xiaomi tablet MI PAD 4 Android version 8.1.0

The above models have been tested successfully

For the basic idea of startup and self startup:

  1. To realize the self startup of app, we use broadcast to realize this requirement. The app first needs to register a broadcast about receiving and sending startup (android.intent.action.BOOT_COMPLETED); Moreover, it must be noted that this broadcast must be statically registered, not dynamically registered (this kind of broadcast that receives the startup broadcast must be statically registered, so that the application can still receive the startup broadcast before it runs
    , dynamic broadcasting will not work.
  2. After the broadcast is statically registered, you can process it in the onReceive method
  3. Key points
    (1) After installing the application, run it once, then shut down and start it again.
    (2) For andorid4 After 0, you need to start the APP once before you can receive the broadcast after startup. The main purpose is to prevent malicious programs
    (3) As far as possible, only one APP of the same device is allowed to realize the function of startup and self startup, otherwise there should be a conflict between the two

Concrete implementation

//Join permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

For the processing in onReceive method, some models do not work. Here are two implementation methods:
1. Start the application according to the package name
2. Specify the class and jump to the corresponding Activity

 * @author Martin-harry
 * @date 2022/3/4
 * @address
 * @Desc Define self starting BroadcastReceiver
public class AutoStartBroadReceiver extends BroadcastReceiver {
    private static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    public void onReceive(Context context, Intent intent) {
        Log.e("Receive broadcast", "onReceive: ");
        Log.e("Receive broadcast", "onReceive: " + intent.getAction());
        //Power on
        if (ACTION.equals(intent.getAction())) {
            Log.e("Receive broadcast", "onReceive: Started...");

//The first method: according to the package name
//            PackageManager packageManager = context.getPackageManager();
//            Intent mainIntent = packageManager.getLaunchIntentForPackage("com.harry.martin");
//            mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(mainIntent);
//            context.startService(mainIntent);

//The second method: specify the class and jump to the corresponding activity
            Intent mainIntent = new Intent(context, LoginActivity.class);
             * Intent.FLAG_ACTIVITY_NEW_TASK
             * Intent.FLAG_ACTIVITY_CLEAR_TOP
//            context.startService(mainIntent);


Register broadcast

                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.HOME" />

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

Scenario test

After completing the above work, here is the permission management operation for some mobile applications

   ComponentName localComponentName = new ComponentName(MyApp.getInstance(), MyInstalledReceiver.class);
   int i = MyApp.getInstance().getPackageManager().getComponentEnabledSetting(localComponentName);
//        Log.e("self start > > > >", "oncreate:" + I);
     * Get the Intent of the self startup management page
     * @param context context
     * @return Management page of Intent returned from Startup
    public static Intent getAutostartSettingIntent(Context context) {
        ComponentName componentName = null;
        String brand = Build.MANUFACTURER;
        Intent intent = new Intent();
        switch (brand.toLowerCase()) {
            case "samsung"://Samsung
                componentName = new ComponentName("", "");
            case "huawei"://Huawei
                Log.e("Self start management >>>>", "getAutostartSettingIntent: Huawei");
                componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");
//            componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");// At present, it is universal
            case "xiaomi"://millet
//                componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity");
                componentName = new ComponentName("","");
            case "vivo"://VIVO
//            componentName = new ComponentName("", "");
                componentName = new ComponentName("", "");
            case "oppo"://OPPO
//            componentName = new ComponentName("", "");
                componentName = new ComponentName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerUsageModelActivity");
            case "yulong":
            case "360"://360
                componentName = new ComponentName("", "");
            case "meizu"://Meizu
                componentName = new ComponentName("", "");
            case "oneplus"://One plus
                componentName = new ComponentName("", "");
            case "letv"://letv 
                intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        return intent;

About authorization operation of Hongmi mobile phone:

Find the mobile housekeeper application in Hongmi mobile phone and open the application management

Open authorization management, set self startup permission for your application, and allow the application to pop up the interface in the background

About Huawei mobile phone authorization:


When I was testing, I had to wait for a period of time for the red rice mobile phone to start automatically. If any boss knows how to solve it, I can comment below. Thank you


In another way, it's best to let the system desktop application start you, Launcher
launcher is an app written in java, but it is a system application. However, if you take this code, it seems that you need fw's help or you can work it out by yourself
Specifically, add an intent in the MainActivity of the Launcher to jump to the package name of your app

Keywords: Java Android

Added by sherrilljjj on Wed, 09 Mar 2022 05:29:31 +0200