How can we ensure that the Service is not killed in the background ?, Service background killing

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.