In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Android background running whitelist how to achieve survival, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Current situation of keeping alive
We know that the Android system will kill the background process, and with the update of the system version, there is a growing trend to kill the process. The system itself has a good starting point, because it can save memory, reduce power consumption, and avoid some rogue behavior.
But there are some applications, the usage scenario of the application itself needs to run in the background, and users are willing to let it run in the background, such as running applications. On the one hand, rogue software uses a variety of rogue means to keep alive, on the other hand, the system increases efforts to kill the background, resulting in some of our applications that really need to run in the background are mistakenly killed, miserable.
Elegant to keep alive?
In order to keep alive, there have been a lot of "cool techs", such as 1-pixel Activity, playing silent audio, two processes guarding each other, and so on. These practices can be said to be very rogue, and even destroyed the ecology of Android, fortunately, with the update of the version of the Android system, many of these unconventional survival methods have become ineffective.
For those applications that really need to run in the background, how can we keep them alive gracefully?
Running whitelist in the background
Since Android 6.0, the system has added hibernation mode in order to save power. After waiting for a period of time, the system will kill the process that is running in the background. However, the system will have a whitelist running in the background, and the applications in the whitelist will not be affected. In the native system, you can see the whitelist through "Settings"-"Battery"-"Battery Optimization"-"unoptimized applications". Usually see the following two:
Next time it is said by the product, "XXX can keep alive, why can't we!" "at that time, you will know how to fight back. Big manufacturers add their applications to the whitelist by default through cooperation with mobile phone manufacturers. If you are in a big factory that can talk about this kind of cooperation, you don't have to look down.
Fortunately, the system has not abandoned us, allowing us to apply to add applications to the whitelist.
First of all, you can determine whether our application is on the whitelist by the following methods:
If you are not on the whitelist, you can apply to be added to the whitelist with the following code:
@ 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;}
When you apply, a window appears on the application:
As you can see, this system pop-up window will have a reminder that affects the battery life, so if you want the user to click to allow it, you must have the relevant instructions. If you want to determine whether the user clicked allow, you can call startActivityForResult when you apply, and determine whether it is on the whitelist again in onActivityResult.
Manufacturer background management
One of the difficulties in Android development is that the major mobile phone manufacturers have made different customizations to the native system, which leads to different adaptations. Background management is a good embodiment. Almost every manufacturer has its own background management, and even if the application is added to the background running whitelist, it may still be killed by the manufacturer's own background management.
If the application can be added to the background management whitelist of the manufacturer's system, the probability of the process being killed can be further reduced. Different manufacturers set up in different places, usually in their own "Mobile Manager", but what is more difficult is that even if the system of the same manufacturer, different versions may be set in different places.
Ideally, we present a picture and text operation step to the user according to different mobile phones, or even different system versions, and provide a button to jump directly to the specified page to set up. But need to adapt to each version of each manufacturer, the workload is relatively large. After testing the mobile phones of most of the mainstream Android manufacturers with real phones, I sorted out the relevant information about some mobile phones.
First of all, 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 of 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);}
The following is the judgment of some mobile phone manufacturers, the jump method and the corresponding setting steps. The jump method does not guarantee the success of the jump on all versions, and all need to add try catch.
Huawei
Manufacturer's judgment
Public boolean isHuawei () {if (Build.BRAND = = null) {return false;} else {return Build.BRAND.toLowerCase () .equals ("huawei") | | Build.BRAND.toLowerCase () .equals ("honor");}}
Jump to Huawei Mobile Manager's startup management page:
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");}}
Procedure: apply Startup Management-> turn off Application switch-> turn on allow self-startup
millet
Manufacturer's judgment
Public static boolean isXiaomi () {return Build.BRAND! = null & Build.BRAND.toLowerCase () .equals ("xiaomi");}
Jump to the self-startup management page of Xiaomi Security Center:
Private void goXiaomiSetting () {showActivity ("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity");}
Procedure: authorization management-> self-startup management-> allow application self-startup
OPPO
Manufacturer's judgment
Public static boolean isOPPO () {return Build.BRAND! = null & Build.BRAND.toLowerCase () .equals ("oppo");}
Jump to OPPO Mobile Manager:
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");}}
Procedure: permission privacy-> self-startup management-> allow application self-startup
VIVO
Manufacturer's judgment
Public static boolean isVIVO () {return Build.BRAND! = null & Build.BRAND.toLowerCase () .equals ("vivo");}
Jump to VIVO Mobile Manager:
Private void goVIVOSetting () {showActivity ("com.iqoo.secure");}
Procedure: permission management-> self-startup-> allow application self-startup
Meizu
Manufacturer's judgment
Public static boolean isMeizu () {return Build.BRAND! = null & Build.BRAND.toLowerCase () .equals ("meizu");}
Jump to Meizu Mobile Manager:
Private void goMeizuSetting () {showActivity ("com.meizu.safe");}
Steps: permission Management-> background Management-> Click apply-> allow backend to run
Samsung
Manufacturer's 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");}}
Procedure: automatically run applications-> turn on Application switches-> Battery Management-> unmonitored applications-> add applications
Letv
Manufacturer's judgment
Public static boolean isLeTV () {return Build.BRAND! = null & Build.BRAND.toLowerCase () .equals ("letv");}
Jump Letv Mobile Manager:
Private void goLetvSetting () {showActivity ("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity");}
Procedure: self-startup management-> allow application self-startup
Hammer
Manufacturer's judgment
Public static boolean isSmartisan () {return Build.BRAND! = null & Build.BRAND.toLowerCase () .equals ("smartisan");}
Jump to mobile phone management:
Private void goSmartisanSetting () {showActivity ("com.smartisanos.security");}
Operation steps: rights management-> self-startup rights management-> Click apply-> allow to be started by the system
In the previous running application, I added a permission settings page to the settings, which included the above and some necessary permission settings for running. Recently, we found that competitor Dong also followed. Figure 1 is done by us, and figure 2 is made by Dong:
Some Dong paid all-round tribute from the design, from the copywriting that I did not write well, and even from the pictures I intercepted from more than a dozen mobile phones. Thank your competitors for your approval, but recently I heard this sentence at a press conference: can you say thank you while paying tribute?
This kind of tribute, on the one hand, shows that the process is easy to be killed and difficult to keep alive, on the other hand, it also shows that this means to guide users to set up the whitelist is effective.
This is the answer to the question about how to keep the whitelist running in the Android background. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.