Android running white list in the background, elegant for life

Keywords: Android Mobile MIUI xml

 

 

Survival Status

We know that there will be background killing on Android systems, and with the update of system versions, there will be an increasing trend in killing processes.The system's starting point is good because it saves memory, reduces power consumption, and avoids some rogue behavior.

But for some applications, the scenario for the app itself needs to run in the background, and users are willing to let it run in the background, such as the running app.On the one hand, rogue software uses a variety of rogue means to survive, on the other hand, the system increases the intensity of killing the background, causing some of our applications that really need to run in the background to be killed by mistake.

Elegant and lively?

In order to survive, there has been a lot of "black technology", such as 1 pixel Activity, playing no audio, two processes guarding each other, and so on.These practices can be said to be hooligans and even damage Android's ecosystem, but with the updating of Android system versions, many of these unconventional lifestyles have failed.

How do we maintain elegance for applications that do need to run in the background?

Background running whitelist

Beginning with Android 6.0, the system has added hibernation mode to save power. After a period of standby, the system will kill the running processes in the background.However, there will be a whitelist running in the background. Applications on the whitelist will not be affected. Under the native system, you can see the whitelist through Settings - Battery - Battery Optimization - Unoptimized Application. You will usually see the following two:

 

 

 

Next time the product says, "XXX can live, why can't we?"Then you know how to get back.By partnering with mobile phone manufacturers, big companies add their own apps to the whitelist by default.If you're in a big factory where you can talk about this kind of collaboration, you don't have to look down.

Fortunately, the system has not abandoned us yet, allowing us to apply for the application to be whitelist.

First, configure permissions in the AndroidManifest.xml file:

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

You can tell if our app is on the whitelist by following these methods:

@RequiresApi(api = Build.VERSION_CODES.M)
private boolean isIgnoringBatteryOptimizations() {
    boolean isIgnoring = false;
    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (powerManager != null) {
        isIgnoring = powerManager.isIgnoringBatteryOptimizations(getPackageName());
    }
    return isIgnoring;
}

If you are not on the whitelist, you can apply to join the whitelist with the following code:

@RequiresApi(api = Build.VERSION_CODES.M)
public void requestIgnoreBatteryOptimizations() {
    try {
        Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

When applying, such a window appears on the application:

 

 

 

You can see that this system pop-up will have an impact on battery life alert, so if you want users to point for permission, you must have a description.If you want to determine if the user clicked Allow, you can call startActivityForResult at the time of application, and in onActivityResult you can tell if it is on the whitelist again.

Manufacturer Background Management

One of the difficulties in Android development is that different mobile phone manufacturers customize their native systems differently, which leads to different adaptions. Background management is a good example.Almost every manufacturer has its own background management, even if the application is added to the background running whitelist, it may still be eliminated by the manufacturer's own background management.

Adding an application to the background management whitelist of a manufacturer's system further reduces the chance of a process being killed.Different vendors set it up in different places, usually in their own "cell phone stewardship", but it's even more difficult that different versions of the same vendor's system may be set up in different places.

Ideally, we present users with a graphical operation step and provide a button to jump directly to the specified page for setup, depending on the phone or even the system version.However, each version of each vendor needs to be adapted, and the workload is relatively large.After I tested most of the major Android manufacturers'phones with real phones, I sorted out some of them.

First, we can define two methods:

/**
 * Jump to the home page of the specified application
 */
private void showActivity(@NonNull String packageName) {
    Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
    startActivity(intent);
}

/**
 * Jump to the specified page for the specified application
 */
private void showActivity(@NonNull String packageName, @NonNull String activityDir) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName(packageName, activityDir));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

Below are the manufacturer's judgments of some mobile phones, the jump method and corresponding setup steps. The jump method does not guarantee a successful jump on all versions, it needs to add try catch.

Huawei

Vendor judgment:

public boolean isHuawei() {
    if (Build.BRAND == null) {
        return false;
    } else {
        return Build.BRAND.toLowerCase().equals("huawei") || Build.BRAND.toLowerCase().equals("honor");
    }
}

Jump to the startup management page of Huawei Mobile Housekeeper:

private void goHuaweiSetting() {
    try {
        showActivity("com.huawei.systemmanager",
            "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
    } catch (Exception e) {
        showActivity("com.huawei.systemmanager",
            "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");
    }
}

Step: Apply Startup Management - > Turn Off Application Switch - > Turn On Allow Self-Starting

millet

Vendor judgment:

public static boolean isXiaomi() {
    return Build.BRAND != null && Build.BRAND.toLowerCase().equals("xiaomi");
}

Jump to the self-start management page of Millet Safety Center:

private void goXiaomiSetting() {
    showActivity("com.miui.securitycenter",
        "com.miui.permcenter.autostart.AutoStartManagementActivity");
}

Step: Authorization Management - > Self-Start Management - > Allow Application of Self-Start

OPPO

Vendor judgment:

public static boolean isOPPO() {
    return Build.BRAND != null && Build.BRAND.toLowerCase().equals("oppo");
}

Jump OPPO Cell Housekeeper:

private void goOPPOSetting() {
    try {
        showActivity("com.coloros.phonemanager");
    } catch (Exception e1) {
        try {
            showActivity("com.oppo.safe");
        } catch (Exception e2) {
            try {
                showActivity("com.coloros.oppoguardelf");
            } catch (Exception e3) {
                showActivity("com.coloros.safecenter");
            }
        }
    }
}

Step: Privacy of Rights - > Self-Start Management - > Allow application to self-start

VIVO

Vendor judgment:

public static boolean isVIVO() {
    return Build.BRAND != null && Build.BRAND.toLowerCase().equals("vivo");
}

Jump to VIVO Cell Housekeeper:

private void goVIVOSetting() {
    showActivity("com.iqoo.secure");
}

Action steps: Permission management - > self-start - > Allow application self-start

Meizu

Vendor judgment:

public static boolean isMeizu() {
    return Build.BRAND != null && Build.BRAND.toLowerCase().equals("meizu");
}

Jump to Charming Mobile Housekeeper:

private void goMeizuSetting() {
    showActivity("com.meizu.safe");
}

Step: Rights Management - > Background Management - > Click Apply - > Allow Background Run

Samsung

Vendor judgment:

public static boolean isSamsung() {
    return Build.BRAND != null && Build.BRAND.toLowerCase().equals("samsung");
}

Jump to Samsung Smart Manager:

private void goSamsungSetting() {
    try {
        showActivity("com.samsung.android.sm_cn");
    } catch (Exception e) {
        showActivity("com.samsung.android.sm");
    }
}

Step: Auto Run Application - > Open Application Switch - > Battery Management - > Unmonitored Application - > Add Application

letv

Vendor judgment:

public static boolean isLeTV() {
    return Build.BRAND != null && Build.BRAND.toLowerCase().equals("letv");
}

Jump Le Video Mobile Housekeeper:

private void goLetvSetting() {
    showActivity("com.letv.android.letvsafe", 
        "com.letv.android.letvsafe.AutobootManageActivity");
}

Step: Self-Start Management - > Allow application of self-start

Hammer

Vendor judgment:

    public static boolean isSmartisan() {
        return Build.BRAND != null && Build.BRAND.toLowerCase().equals("smartisan");
    }

Jump Mobile Management:

private void goSmartisanSetting() {
    showActivity("com.smartisanos.security");
}

Step: Rights Management - > Self-Starting Rights Management - > Click Apply - > Allow System Startup

Friends salute?

In the previous run app, I added a permission settings page to the settings to put the above settings in it.Recently, it was found that friends and businessmen have followed up. Fig. 1 is what we did, and Fig. 2 is what someone did:

 

 

Someone from the design, from the bad copy I wrote, or even from the pictures I cut off one by one from more than a dozen mobile phones, paid a full tribute.Thank you for your acceptance, but I recently heard a sentence at a conference: While saluting, can you say a thank you?

One salute, on the one hand, shows that there is a real problem that the process is easy to be killed and difficult to survive, on the other hand, it also shows that this kind of means to guide users to set up a whitelist is effective.

198 original articles were published. 54 were praised. 240,000 visits+
Private letter follow

Posted by zgkhoo on Sat, 18 Jan 2020 17:55:45 -0800