How to get your Android program to never be killed by the system
Related knowledge:
In general, in an Android System, when a process is inactive for a long time, or if the system resources are tight, the process may be killed by the system to reclaim some resources. the Android system will selectively kill some processes based on the priority of the process, from high to low priority:
1. Foreground processes (Foregroundprocess)
2. Visual processes (visibleprocess)
3. Service process
4. Background processes (Backgroundprocess)
5. Empty process
1. Foreground processes (Foregroundprocess) refer to the current active process, which is the application process that has windows, controls, and is interacting with the user. Android will try to reclaim system resources from other processes to keep its response.
2. The visible processis the visible activity process, but it is not currently running in the foreground and cannot be reflected on user events. For example, a program a is running in the foreground, then another program bis started,program b 's window to the program a window is completely or partially obscured, program a now belongs to the visible process. In general, only if the system resources are scarce, the system terminates it in order to ensure the operation of the active process.
3.Service Process (Service Processrefers to processes that do not have a visible interface, and they cannot interact directly with the user. The foreground process (usually aActivityIs paused when it becomes inactive, the service process can remain running in the background, so if you want to keep the process running after you go back to the background, you must start aService. But as soon as the foreground process needs resources,ServiceIt's easy for the system to giveKilloff. Typically, by defaultServicewasKillautomatically restarts after a reboot, but does not guarantee that the previous environment will be restoredServiceAuto restart, callstopself ()function.
4. background processes (Backgroundprocess) refer to processes that are not visible and do not have any active services running. Usually the background process is much more, andAndroid will terminate them in the "last seen, first stop" way to provide resources for the foreground process.
5. for empty processes,Android to improve the overall performance of the system, often after the end of the lifetime of the application still keep them in memory, when the program is started again, you can speed up the boot speed. Such processes are periodically terminated as needed.
From the above, think of what you have writtenServiceIt is not easy for the system to recycle resourcesKillThe normal practice is to maximize the priority of the program, such as callingStartforeground (True). BecauseServiceOn startup, the default is marked asBackground, the currently runningActivitybe marked asForeground, when theServiceset toForeground, its priority is the same as the runningActivitysimilar, but this does not guarantee that it will not beKill.
What if we were going to write a monitoring program to monitor the operation of other programs? We must ensure that our programs do not killby Recycling resources. Clearly, the priority 2-5 process is unreliable, and as a monitoring program, its priority cannot be 1.
It seems that there is no way to do it ... Don't worry, keep looking down ...
Workaround:
Simply put, the service you write is turned into a Core service, and the program you write is promoted to a system-level program so that in no case will it be killed off.
To write your own service promotion as a Core service, the application needs to set two flags:flag_persistent and flag_system.
The following are the specific practices:
1. Set flag_persistent: In the androidmanifest.xml file, add android:persistent= "true";
2. set flag_system: Put your written application in the /system/app/ directory. Here's how:
A. Connect to ADB andexecute the following two commands:
ADB remount
ADB push your.apk/system/app/
The command adb remount is to obtain temporary operation permissions for the system directory, and then use the push command to upload its own package to the System application directory.
B. Restart your phone. After booting, you can see the program you wrote in the app list.
It is necessary to note that the System program does not use the adb install your.apk as a non-system program for installing the apk Package, we just need to put the apk Copy the package to the /system/app/ directory to reboot. If you use the adb install method, then the flag_system flag will not be set,flag_persistent logo will not have any effect.
C. If you need to uninstall your own written system application, execute the following command:
ADB remount
ADB Shell rm/system/app/yourpackage.apk
Also, it should be noted that if you want to add your own written system-level application in the emulator, as if it is not possible, because the apk copied to the /system/app/ , but also must restart, but the simulator restarts, The system directory will also revert to its original state. But online seems to have been done, the method is also very simple, is the Android source code corresponding place slightly changed, and then recompile ...
Now, we can get our program to do whatever it is.
How to get your Android program to never be killed by the system