Anger hate Product Manager: Achieving elegant background have difficulty keeping alive? nonexistent!

Anger hate Product Manager: Achieving elegant background have difficulty keeping alive?  nonexistent!

Keep alive the status quo

We know, Android system will kill the presence of background processes, and with the updated version of the system, as well as efforts to kill the process of growing trend. This approach system itself is a good starting point, because you can save memory, reduce power consumption, but also to avoid some of hooliganism.

But part of the application, the application's own usage scenarios need to run in the background, the user is willing to let it run in the background, such as running type applications. On the one hand rogue software rogue keep alive with a variety of means, on the other hand system increase efforts to kill the background, resulting in a number of applications that we really need to run in the background manslaughter, miserable.

Elegance keep alive?

In order to do keep alive, there have been many "Black Technology", such as Activity 1 pixels, silent audio playback, such as the double process guard each other. These practices can be said to be a rogue, even destroy the ecology of Android, fortunately with the Android system version update, these unconventional means to keep alive many are ineffective.

For those who really need to run applications in the background, how do we keep alive the elegance of it?

Background white list

Starting Android 6.0, the system in order to increase the power saving sleep mode, the system standby after a period of time, it will kill the process running in the background. But the system will be running in the background a white list, white list where the application will not be affected, in the native system, through the "Settings" - "Optimizing battery" - - "Battery", "non-optimized applications," you can see this white list, usually you see the following two:

Anger hate Product Manager: Achieving elegant background have difficulty keeping alive?  nonexistent!

Next is the product say "XXX can keep alive, why do not we!" When you know how to hate back. Manufacturers through collaboration and mobile phone manufacturers, their applications will default added to the whitelist. If you can talk into a giant of this cooperation, I will not look down.

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

First, look at the configuration in the AndroidManifest.xml file permissions:

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

Through the following methods to determine whether our application in the white list:

@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 not in the white list, the following code can be applied to join the whitelist:

@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 for such a window will appear on the applications:

Anger hate Product Manager: Achieving elegant background have difficulty keeping alive?  nonexistent!

可以看到,这个系统弹窗会有影响电池续航的提醒,所以如果想让用户点允许,必须要有相关的说明。如果要判断用户是否点击了允许,可以在申请的时候调用 startActivityForResult,在 onActivityResult 里再判断一次是否在白名单中。

厂商后台管理

Android 开发的一个难点在于,各大手机厂商对原生系统进行了不同的定制,导致我们需要进行不同的适配,后台管理就是一个很好的体现。几乎各个厂商都有自己的后台管理,就算应用加入了后台运行白名单,仍然可能会被厂商自己的后台管理干掉。

如果能把应用加入厂商系统的后台管理白名单,可以进一步降低进程被杀的概率。不同的厂商在不同的地方进行设置,一般是在各自的「手机管家」,但更难的是,就算同一个厂商的系统,不同的版本也可能是在不同地方设置。

最理想的做法是,我们根据不同手机,甚至是不同的系统版本,给用户呈现一个图文操作步骤,并且提供一个按钮,直接跳转到指定页面进行设置。但需要对每个厂商每个版本进行适配,工作量是比较大的。我使用真机测试了大部分主流 Android 厂商的手机后,整理出了部分手机的相关资料。

首先我们可以定义这样两个方法:

/**
 * 跳转到指定应用的首页
 */
private void showActivity(@NonNull String packageName) {
    Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
    startActivity(intent);
}

/**
 * 跳转到指定应用的指定页面
 */
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);
}

以下是部分手机的厂商判断,跳转方法及对应设置步骤,跳转方法不保证在所有版本上都能成功跳转,都需要加 try catch。

华为

厂商判断:

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

跳转华为手机管家的启动管理页:

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");
    }
}

操作步骤:应用启动管理 -> 关闭应用开关 -> 打开允许自启动

小米

厂商判断:

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

跳转小米安全中心的自启动管理页面:

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

操作步骤:授权管理 -> 自启动管理 -> 允许应用自启动

OPPO

厂商判断:

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

跳转 OPPO 手机管家:

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");
            }
        }
    }
}

操作步骤:权限隐私 -> 自启动管理 -> 允许应用自启动

VIVO

厂商判断:

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

跳转 VIVO 手机管家:

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

操作步骤:权限管理 -> 自启动 -> 允许应用自启动

魅族

厂商判断:

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

跳转魅族手机管家:

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

操作步骤:权限管理 -> 后台管理 -> 点击应用 -> 允许后台运行

三星

厂商判断:

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

跳转三星智能管理器:

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

Procedure: AutoRun application -> Open Switch Applications -> Battery Management -> Application unmonitored -> Add Application

TV Plus

Manufacturer judgment:

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

Jump music as phone butler:

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

Steps: since the launch of Management -> Allow application from the start

hammer

Manufacturer judgment:

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

Jump phone management:

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

Steps: Rights Management -> since the start Rights Management -> Click Applications -> Allow the system to start

Friends of the tribute?

In doing before running the application, I added a permissions page in the settings, the above-mentioned set on the inside. Friends of the recent discovery of a boom followed suit, and Figure 1 is what we do, Figure 2 is a boom to do:

Anger hate Product Manager: Achieving elegant background have difficulty keeping alive?  nonexistent!

From the design of a boom, from what I wrote enough good copy, I even cut one by one from the phone down on a dozen diagram, a full range of tribute. Thank approved a boom, but recently heard in a conference such a sentence: a tribute at the same time, I can not say thank you?

Tribute to a boom on the one hand shows the current process does exist easily killed, Bao Dai live difficulty of the problem, on the other hand also shows that this means guides the user whitelist setting is effective.

end

In fact, for programmers, content knowledge to learn, there are too many technical, environmental order not to be eliminated only improve ourselves, always us to adapt to the environment, not the environment to adapt to us!

Here attached the above-mentioned technical system diagram related to dozens of sets of Tencent, headlines, Ali, the US group and other companies face questions 19 years , the technology has become a finishing point video and PDF (in fact, spend a lot of time than expected) including knowledge context + many details , because of space limitations, here in the form of pictures to show you part of it.

I believe it will bring you a lot of harvest:

Anger hate Product Manager: Achieving elegant background have difficulty keeping alive?  nonexistent!

Anger hate Product Manager: Achieving elegant background have difficulty keeping alive?  nonexistent!

[HD brain diagram above], and [supporting] PDF technology architecture can add me wx: X1524478394 free access!

When programmers easily, when a good programmer is a need to learn from junior programmer to senior programmer, architect from primary to senior architect, or to management, technical director from technical manager to each stage We need to have different capabilities. Early to determine their career direction, in order to throw off their peers at work and in capacity building.

Guess you like

Origin blog.51cto.com/14332859/2464026