Android Service keeps not being killed

Source: Internet
Author: User

During Android development, the Onstartcommand (Intent,int,int) method of the service object is called every time StartService (Intent) is called. Then do some processing in the Onstartcommand method.

From the official Android document, we know that Onstartcommand has 4 kinds of int return values, first of all, the function of the int return value.


Onstartcommand has 4 kinds of return values:

Start_sticky: If the service process is killed, the status of the reserved service is the start state, but the delivery 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.

Ii. Creating a service that is not killed

1. In the service override the following method, this method has three return values, Start_sticky (or start_sticky_compatibility) is the service is killed after the automatic override created

@Override
public int Onstartcommand (Intent Intent, int flags, int startid)
{
return start_sticky_compatibility;
Return Super.onstartcommand (Intent, flags, Startid);
}

Or

@Override
public int Onstartcommand (Intent Intent, int flags, int startid)
{
Flags = Start_sticky;
Return Super.onstartcommand (Intent, flags, Startid);
return start_redeliver_intent;
}

@Override
public void OnStart (Intent Intent, int startid)
{
Register the broadcast again dynamically
Intentfilter localintentfilter = new Intentfilter ("Android.intent.action.USER_PRESENT");
Localintentfilter.setpriority (Integer.max_value);//Shaping maximum value
Myreceiver searchreceiver = new Myreceiver ();
Registerreceiver (Searchreceiver, Localintentfilter);

Super.onstart (Intent, Startid);
}


2. Restart the service in OnDestroy () of the service.

public void OnDestroy ()
{
Intent localintent = new Intent ();
Localintent.setclass (this, myservice.class); Restart Service upon destruction
This.startservice (localintent);
}

3. Create a broadcast

public class Myreceiver extends Broadcastreceiver
{
@Override
public void OnReceive (context context, Intent Intent)
{
Context.startservice (New Intent (context, google.class));
}
}

4.androidmanifest.xml registered broadcast Myreceiver and MyService services

<receiver android:name= ". Myreceiver" >
<intent-filter android:priority= "2147483647" ><!--priority plus-
<!--when the system starts, it is called-
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
<!--will be called when the unlock is complete
<action android:name= "Android.intent.action.USER_PRESENT"/>
<!--monitor Scene toggle
<action android:name= "Android.media.RINGER_MODE_CHANGED"/>
</intent-filter>
</receiver>

<service android:name= ". MyService ">

Note: Unlock, start, switch scenes to activate the broadcast need to add permissions, such as Start-up completion, and mobile phone status.

<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>


Pro-Test ZTE U795 Mobile Android 4.0.4 version of ADB push to System\app android:persistent= "true"
Become the core program, in the 360 kill process, myreceiver still effective, ensure service rebirth. Uh

Kill question:
1. Stop Service in Settings
In the OnDestroy method, call StartService to restart the service.
Force stop app in 2.settings
Capture system for broadcast (action is Android.intent.action.PACKAGE_RESTARTED)
3. Kill running task with a third-party app
Elevate service priority, program signature, or ADB push to system\app inferior


Applications placed under/system/app enjoy more privileges than/data/app applications, such as setting the persistent property to true in their manifest.xml files, which can be protected from out-of-memory killer. Androidmanifest.xml files such as the application ' Phone ':
<application android:name= "Phoneapp"
Android:persistent= "true"
Android:label= "@string/dialericonlabel"
android:icon= "@drawable/ic_launcher_phone" >
...
</application>
App promotion to system core level after Setup

Android Service keeps not 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.