How can we ensure that the Service is not killed in the background ?, Service background killing
I. Basic knowledge reserve in the Early Stage
(1)Why do we need to ensure the backend Service?Not killed?
Improve the sense of presence of applications. For large factory applications, the program "live" is not a problem, but in order to bring a better user experience and improve user stickiness, it is necessary to call more services of the program as much as possible, this will bring more user experience (not necessarily better, but the number is dominant). For small families, it is their top priority to make programs "alive" as much as possible, using background services to keep applications alive on users' mobile phones is also a way to survive.
(2)Background ServiceAre you sure you don't want to die??
Of course not. Any application service can be killed, and the system directly kills the service, or some users directly kill the service, therefore, the actual meaning of ensuring that the Service is not killed is to ensure that the Service can be restarted immediately after being killed, which is called the "pseudo-undead" state. This article summarizes five common methods to ensure "pseudo-undead.
Ii. Code above, specific implementation
①Modifies the return value of the onStartCommand (...) method.:
According to the official documentation, the return value of the onStartCommand (...) method is changedSTART_STICKYYou can restart the service after the service is stopped.
@Override public int onStartCommand(Intent intent, int flags, int startId){ flags = START_STICKY; return super.onStartCommand(intent, flags, startId); // return START_REDELIVER_INTENT; }
②Improve service priority:
In the AndroidManifest. xml fileAndroid: priority = "1000"This attribute sets the highest priority. 1000 is the highest value. If the number is smaller, the higher the priority is, and it is applicable to broadcast.
③Improve service Process Priority:
Although the Service does not rely on activity to survive, it depends on the application process. If the application process is disabled, the Service must be destroyed. Therefore, try to improve the process of Service attachment, it can also ensure its survival status. There are six processes in the "frontend visible process serves blank processes in the background". If the service is to be longer, you can set itFront-End Service, Attached to user interactionForeground Process.
④Restart the service in the onDestroy Method:
Public void onDestroy () {Intent localIntent = new Intent (); localIntent. setClass (this, MyService. class); // Restart Service this. startService (localIntent) Upon destruction );}
⑤Add the Persistent attribute to the Application.: Add the following content to the application tag in androidmanifest. xml:Android: persistent = "true"? The process where the application is located will not be killed by the LMK. But there is a premise thatThe application must be a system application.That is to say, applications cannot be installed normally. You must put the apk package of the application directly in the/system/app directory. It takes effect only after the system is restarted.