This article is just a summary of the historical preservation scheme of Android, without any special reference significance. Android has reached 10, and 100% preservation itself no longer exists. All the schemes in the article may be useful, after all, there are still people using 4.4. Can you not write code?
Saving is usually divided into two schemes, one is to improve the process priority and prevent the process from being killed, the other is to kill the process
1. Process priority
The Android system tries to keep application processes alive as much as possible, but when it needs to create new processes or run more important ones, it will recycle lower-priority processes, which is what LowMemoryKiller does. The process priority is /proc/pid/oom_adj
Prioritization of processes
- Foreground Process
- Visible Process
- Service Process
- Background Processes
- Empty Processes
Foreground process
- Has an Activity that the user is interacting with (in onResume)
- Have a Service bound to the Activity that is in onResume
- Have Service call startForeground become foreground Service
- Have services performing life cycle callbacks (onCreate, onStart, onDestroy)
- Owning BroadcastReceiver is executing onReceive
Visible process
- Having an Activity in onPause that is visible but not actionable
- Have a Service bound to the Activity that is in onPause
Service process
- The Service started only by startService
Background processes
- Have an Activity in onStop
An empty process
- Does not own any active component processes
2. Recycling strategy
From the Zygote fork out process will be stored in ActivityManagerService. MLruProcesses list, by ActivityManagerService for unified management. ActivityManagerService updates the oOM_adj value of a process based on the process status. When the memory reaches a certain threshold, processes with high OOM_ADJ are cleared.
Refer to the blog
3. Survival programme
3.1 Improving the Process Priority
3.1.1 the use of the Activity
1 pixel Activy, monitor the event of unlocking the screen of the mobile phone, destroy the Activity when unlocking, start the Activity when locking the screen, and remove it from the RecentTask without feeling
3.1.2 Foreground Service +Notification
The Service is started by startForegroundService. In earlier versions, the Notification can be hidden in a special way. In later versions, the Notification cannot be evaded
3.1.3 Enable battery management to enable background running of applications
At present, the mobile phone in the market, more or less all have the process management optimization, may have the function that allows the application background to allow, but each mobile phone entrance is different, and the different version of the same manufacturer will be different
Specifically, find the battery management of the mobile phone or the background management of the system, make written reminders for different mobile phones, remind users to open this function, violence can find a way to get the specific class name of the Activity package name and other information, reflection call.
This scheme should not be used in general applications, the workload is huge, and it is only suitable for reminding applications, such as taking medicine reminder, wake up alarm clock, these applications are very high requirements for survival
3.2 A process is Pulled alive after it dies
3.2.1 Listening for static broadcast of the system
In earlier versions, static broadcast can wake up the application process, so listening for system broadcast, such as boot, lock screen, unlock, etc., can be done, but in older versions, static broadcast listening system can not be broadcast
3.2.2 Listening for Three-party Static Broadcasts
Similar to the last scheme, are the use of static broadcast can pull live application as the basis, but the sender is not the system, and three applications. Therefore, this scheme is feasible, but it is very unstable. Different overseas and domestic user groups will use different APK for mobile phones, and a large number of decompilation tripartite applications are required, with high investment costs
3.2.3 Using the System Service mechanism
If the onStartCommand value of Service is START_STICKY or START_REDELIVER_INTENT, the Service is automatically restarted. However, if the Service is killed for five times in a short period of time, the Service will not be restarted
3.2.4 using JobScheduler
JobScheduler was introduced after Android 5.0 and is essentially a scheduled task. If a process is killed, the task will still be executed. After 7.0, JobScheduler added a limit with a minimum interval of 15 minutes. However, there is still a chance that the process will not trigger after it dies.
3.2.5 use AlarmManager
By essentially setting up a scheduled task, if the process is killed, the task will still be executed and the process can be pulled alive. The Doze mode will affect AlarmManager not to be triggered, so use setAlarmClock to set AlarmManager. It is also possible that the process will not trigger after it dies.
And Android 9.0 Google native phone, more than a function, is to display the phone’s next alarm clock time, if used in this way, the user also noticed this function, then the alarm clock time will be exposed to the application in the blatant preservation
3.2.6 Using the Account synchronization mechanism
The Account synchronization mechanism of the Android system periodically synchronizes accounts. This solution uses the synchronization mechanism to pull and live processes. Add the account and set the synchronization period of the code, Google store will check this scheme, the consequences do not know, recommend caution
Code reference link
3.2.7 Use Native process to pull
The fork mechanism in Linux is used to create a Native process, and the survival of the main process is monitored in the Native process. When the main process dies, the main process is pulled in the Native process immediately.
Aware main process death: Create a monitor file in the main process and hold the file lock in the main process. The file lock application will be blocked after the pull process starts. Once the lock is successfully obtained, it indicates that the main process is down and the pull process can be started.
Pull and activate the main process: Pull and activate the main process using the Native process. Part of the code is as follows, that is, pull and activate the main process using the AM command. Pull the master process in the forestop state by specifying the “-include-stopped -packages” parameter.
However, Android5.0 and above will kill all processes in turn, as well as Native processes
3.2.8 Using the Dual-process Pull Function
Start two services A and B, in different processes, bind B to A’s onStartCommand, and B to A’s onStartCommand. OnServiceDisconnected with the ServiceConnection callback, when the binding is disconnected, another process dies, so restart the dead Service. After 6.0, there will be limited keepalive effects, similar to the Natvie process. There is only a chance to pull alive between kills
3.3 Other pull modes
3.3.1 Use official system services or third-party services
- Firebase’s cloud push is available abroad
- Services such as aurora Push can be used in China
It mainly depends on the use of the same SDK for my application and other applications, and the mutual wake-up function is built into the same SDK. The specific effect of preserving vitality also depends on the ability of the SDK of the three parties