How does Android prevent a service from being killed?

Source: Internet
Author: User

1. re-write the following method in service. This method has three return values. start_sticky indicates that the service is automatically overwritten after it is killed.
@ Override
Public int onstartcommand (intent, int flags, int startid ){
Return start_sticky;
}----------------
@ Override
Public int onstartcommand (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 the service upon destruction
This. startservice (localintent );
}---------------------------------------------
When a process is killed by a QQ Butler, the system's built-in mandatory kill function (in settings) is called. During the kill process, the entire process of the application is stopped, of course, if the service is forced to kill in running, the process is still displayed. Neither kill the entire process nor kill the service that falls into the application, nor restart the service. I don't know how you restarted it...
PS: In eclipse, when the stop button is used to kill the process, the service will be restarted.
Kill:
1. Stop Service in settings
In the ondestroy method, call startservice to restart the service.
2. Force Stop Application in settings
Capture System broadcast (action is Android. Intent. Action. package_restarted)
3. Kill the running task with a third-party Application
Improve Service priority
--------------------------------------------
Start Service

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

  • Today, we will mainly discuss how android can enable a service to automatically start upon startup. The Android mobile phone will trigger a standard broadcast action during startup, called Android. intent. action. boot_completed (remember to trigger only once). Here we can build a broadcast receiver to receive this action. below is a simple example of the following implementation steps:
  • Step 1: Create a broadcast receiver and reconstruct its abstract method onreceive (context, intent) to 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 {
    • // Override the onreceive Method
    • @ Override
    • Public void onreceive (context, intent ){
    • // The xxx. class at the end is the service to be started.
    • Intent service = new intent (context, xxxclass );
    • Context. startservice (service );
    • Log. V ("tag", "automatic Service Startup .....");
    • // Start the application. The parameter is the package name of the application to be automatically started.
    • Intent intent = getpackagemanager (). getlaunchintentforpackage (packagename );
    • Context. startactivity (intent );
    • }
    • }
  • Step 2: configure the XML file and add the intent-filter configuration in the receiver.
  • <Cycler Android: Name = "bootbroadcastreceiver">
  • <Intent-filter>
  • <Action Android: Name = "android. Intent. Action. boot_completed"> </Action>
  • <Category Android: Name = "android. Intent. Category. launcher"/>
  • </Intent-filter>
  • </Cycler>
  • Step 3: add permissions <uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>


---------------------------------------------------
How to implement a process that will not be killed
According to the android documentation, when a process is not active for a long time or the system needs resources, the portal is automatically cleared to kill processes of services and invisible activities.
But if a process does not want to be killed (such as a data cache process, status monitoring process, or remote service process), what should we do to prevent the process from being killed.

Add Android: Persistent = "true" into the <Application> section in your androidmanifest. xml

Remember, this should not be abused. If the system uses this service and there are more apps, the whole system will be finished.
Currently, phone and other very limited applications in the system must be used for trial.

------------------------------------------------
How to increase service priority

The Android system has its own method for memory management. In order to ensure the orderly and stable operation of the system, the system automatically allocates memory to control the memory usage of the program. When the system finds that the current resources are very limited, it will kill programs or services that he thinks are not important to ensure that some programs with higher priority can run. This ensures that the programs that are truly useful to users are still running. If your service encounters this situation, it will probably be killed first. However, if you increase the service priority, you can leave it for a while. We can use setforeground (true) to set the service priority.

Why is it foreground? By default, the started service is marked as background, and the currently running activity is usually marked as foreground, that is to say, if you set foreground for the service, the priority of the running activity will be improved. This does not guarantee that your service will never be killed, but increases the priority of your service.

Starting from Android 1.5, A started service can call startforeground (INT, notification) to set the service to the foreground state, and call stopforeground (Boolean) to set the service to the background state.

We will call startforeground (INT, notification) to pass in the parameter notification, which will display the ongoing foreground service in the status bar. The background service is not displayed in the status bar.

In Android 1.0, set a service to the foreground status:
Setforeground (true );
Mnm. Y (ID, notification );
Set a service to the background status:
Mnm. Cancel (ID );
Setforeground (false );
The comparison shows that calling setforeground (Boolean) in the 1.0 API simply changes the service status and the user will not be aware of it. The new API forcibly binds the notification and action that changes the service status. The foreground service is displayed in the status bar, but the background service is not.

Remote service Controller & binding
Call the service across processes. We will not study it for the moment.
-------------------------------------------------------
How to Prevent services in Android applications from being recycled by the system?Many of my friends are asking how to prevent services in Android applications from being recycled by the system? The following is a simple answer.

For the service to be recycled by the system, the general approach is to improve the priority of the solution, in androidmanifest. for intent-filter in XML files, you can set the highest priority through the Android: Priority = "1000" attribute. 1000 is the highest priority. If the number is smaller, the higher the priority is, and it is useful for broadcasting, if your application is very important, we recommend that you use intent actions to trigger it.

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.