How to get the service to restart automatically without being killed

Source: Internet
Author: User

Override the service's Onstartcommand method. @Override    public int Onstartcommand (Intent Intent, int flags, int startid) {       & Nbsp;return start_sticky;   }    Simple Introduction to this method, in the process of Android development, each time you call StartService (Intent), Calls the service object's Onstartcommand (Intent,int,int) method, and then does some processing in the Onstartcommand method. Then we notice that this function has an int return value, and this article simply tells the effect of the int return value.     from the official Android documentation, we know that Onstartcommand has 4 return values:    Start_sticky: If the service process is killed, The status of the reserved service is the start state, but the delivery of the intent object is not preserved. The system then tries to recreate the service, and since the service status is started, the Onstartcommand (Intent,int,int) method must be called after the services are created. If no startup commands are passed to the service during this time, then the parameter intent will be null.     Start_not_sticky: "Non-sticky". When this return value is used, the service is not automatically restarted if the service is killed by an exception after Onstartcommand is executed.     Start_redeliver_intent: Retransmission INTENT. With this return value, if the service is killed by an exception after Onstartcommand is executed, the service is automatically restarted and the value of intent is passed in. Start_sticky_compatibility:start_sticky compatible version, but does not guarantee that the service will be restarted after kill. Of course there are other solutions, but more or less there will be some drawbacks or relatively troublesome. Here are a few of the most common examples: 1. Restart the service in the Ondestory method, which is generally possible. But if so----"set--download-Force stop. The Ondestory method will not be executed, or it will not invoke the service's Ondestory method if it is used by someone else, such as 360 to kill my app directly. 2. Modify Androidmanifest.xml<manifest  xmlns:android= "Http://schemas.android.com/apk/res/android"         android:shareduserid= "Android.uid.system" >     < Application android:icon= "@drawable/icon" android:label= "@string/app_name" Android:allowclearuserdata= "false"           android:process= "System"   android:killafterrestore= "false" > If this part of the code is added, indicating that the program is running in the system process group, the system process group does not have access to the SD card, and the service is not automatically restarted. 3. Improve service priority, regardless of how high your service priority is, users can kill manually.

There are many other ways to do this, and this is not listed here.

So if you want to keep your service running all the time, the simplest way is to rewrite the Onstartcommand method. But don't do bad things, do not be despised by users of malicious programs

How to get the service to restart automatically without being killed

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.