How Android keeps the service from being killed

Source: Internet
Author: User

1. In the service override the following method, this method has three return values, Start_sticky is the service was killed after the automatic override created
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
return start_sticky;
}----------------
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
TODO auto-generated Method Stub
LOG.V ("Trafficservice", "Startcommand");

Flags = Start_sticky;
Return Super.onstartcommand (Intent, flags, Startid);
return start_redeliver_intent;
}
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);
}---------------------------------------------
When using the QQ Butler to kill the process, the call is the system comes with the mandatory kill function (that is, settings), in Kill, will be applied to the entire process to stop, of course, including service, if the service is forced to kill in running, the display process is still in. The service is not restarted whether the entire process is kill or if only kill falls into the service of the application. I do not know how you realize the restart, is really puzzled ...
PS: In eclipse, when you kill a process with the Stop button, it will restart the service
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
--------------------------------------------
Service boot up

http://blog.csdn.net/flying_vip_521/article/details/7053355

  • Today we are mainly to explore how Android makes a service boot auto-start feature implementation. Android phone will trigger a standard broadcast Action in the boot process, the name is Android.intent.action.BOOT_COMPLETED (remember it will only trigger once), Here we can receive this action by building a broadcast receiver. Let's just write down the following steps to implement:
  • The first step: Create a broadcast receiver, refactor its abstract method OnReceive (context context, Intent Intent), where you start the service or app you want to start.

    • Import Android.content.BroadcastReceiver;
    • Import Android.content.Context;
    • Import android.content.Intent;
    • Import Android.util.Log;
    • public class Bootbroadcastreceiver extends Broadcastreceiver {
    • Overriding the OnReceive method
    • @Override
    • public void OnReceive (context context, Intent Intent) {
    • The xxx.class behind is the service to start.
    • Intent service = new Intent (context,xxxclass);
    • Context.startservice (service);
    • LOG.V ("TAG", "Boot automatic service auto start ...");
    • Launch the app with the package name of the app that needs to start automatically
    • Intent Intent = Getpackagemanager (). Getlaunchintentforpackage (PackageName);
    • context.startactivity (Intent);       
    • }
    • }
  • Step Two: Configure the XML file to receive this add Intent-filter configuration in Receiver
  • <receiver android:name= "Bootbroadcastreceiver" >
  • <intent-filter>
  • <action android:name= "Android.intent.action.BOOT_COMPLETED" ></action>
  • <category android:name= "Android.intent.category.LAUNCHER"/>
  • </intent-filter>
  • </receiver>
  • Step Three: Add Permissions <uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>




---------------------------------------------------
How to implement a process that will not be killed
Look at the Android documentation to know that when the process is inactive for a long time, or when the system needs resources, it automatically cleans up the portal, kills some of the service, and the invisible activity of the process.
However, if a process does not want to be killed (such as a data caching process, or a status monitoring process, or a remote service process), what should be done to keep the process from being killed.

add android:persistent= "true" into the <application> sections in your Androidmanifest.xml

Remember, this can not be abused, the system with this service,app a lot, the whole system is finished.
Currently the system has phone and so very limited, must have been alive application in the trial.

------------------------------------------------
ways to improve service priority

The Android system has its own method for memory management, in order to guarantee the orderly and stable operation of the system, the internal system will automatically allocate and control the memory usage of the program. When the system feels that the current resources are very limited, in order to ensure that some high-priority programs can run, it will kill some of the programs or services that he considers unimportant to free memory. This will ensure that programs that are really useful to the user are still running again. If your Service is in this situation, you will probably be killed first. But if you increase the priority of the service to let him stay a little more, we can use Setforeground (true) to set the service priority.

Why the foreground? The service that is started by default is marked as background, and the activity that is currently running is generally marked as foreground, which means that you set the service to foreground so that he has the same priority as the running activity. A certain increase. When this doesn't guarantee that your Service will never be killed, it just increases his priority.
Starting with Android 1.5, a service that has been started can call Startforeground (int, Notification) to set the service to foreground state, calling Stopforeground ( Boolean) to place the service in the background state.
We will pass in the parameter Notification in the call startforeground (int, Notification), which will show the ongoing foreground service in the status bar. Background service is not displayed in the status bar.
In Android 1.0, set a service to the foreground state:
Setforeground (TRUE);
Mnm.notify (ID, notification);
Set a service to background state:
Mnm.cancel (ID);
Setforeground (FALSE);
In contrast, calling Setforeground (Boolean) in the 1.0 API simply alters the state of the service, and the user is not aware of anything. The new API enforces binding of notification and changing the service state, foreground service is displayed in the status bar, and background service does not.
Remote Service Controller & Binding
Call service across processes. Not studied for the time being.
-------------------------------------------------------
How do I prevent the service from being reclaimed by the Android app?Many friends are asking, how to prevent the service in the Android app from being reclaimed by the system? Here's a quick answer.

For service to be reclaimed by the system, the general practice is to improve the priority can be resolved, in the Androidmanifest.xml file for Intent-filter can be android:priority = "1000" This property to set the highest priority, 1000 is the highest value, if the number is smaller, the lower the priority, while practical for the broadcast, recommend everyone if your application is important, you can consider the system commonly used intent action to trigger.

How Android keeps the service from being killed

Related Article

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.