How Android keeps the service from being killed-increasing process priority
Source: Internet
Author: User
<span id="Label3"></p><p><p><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-size: small;">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</span></span></span></span></span><span><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-size: small;"><br><br></span></span></span></span></span></span></p></p> <ol> <ol> <li>@Override</li> <li>public int Onstartcommand (Intent Intent, int flags, int Startid) {</li> <li>Return start_sticky;</li> <li>}----------------</li> <li>@Override</li> <li>public int Onstartcommand (Intent Intent, int flags, int Startid) {</li> <li>TODO auto-generated Method Stub</li> <li>LOG.V ("trafficservice", "startcommand");</li> <li></li> <li></li> <li>Flags = start_sticky;</li> <li>return Super.onstartcommand (intent, flags, startid);</li> <li>Return start_redeliver_intent;</li> <li>}</li> </ol> </ol>Copy Code<p><p><span><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-size: small;"><br>2. Restart the service in OnDestroy () of the Service.<br><br></span></span></span></span></span></span></p></p> <ol> <ol> <li>public void OnDestroy () {</li> <li>Intent localintent = new Intent ();</li> <li>Localintent.setclass (this, myservice.class); Restart Service upon destruction</li> <li>This.startservice (localintent);</li> <li>}</li> </ol> </ol>Copy Code<p><span><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span> <span style="font-family: song body;"><span style="font-size:small;"> <br> When the QQ Butler killed the process, Called the system comes with the Force kill function (that is, settings), at kill, the entire application process will be stopped, of course, including service, if the service is forced to kill in running, the display process is still there. The service is not restarted whether the entire process is kill or if only kill falls into the service of the Application. Do not know how you achieve the restart, is really puzzled. In eclipse, when you kill a process with the stop button, the service is restarted; <br> <br> Kill problem: <br> <br> 1. settings Stop service <br> In the <br> OnDestroy method, call StartService to restart the Service. <br> <br> 2.settings Force stop applies <br> <br> Capture system for broadcast (action is Android.intent.action.PACKAGE_RESTARTED) <br> <br> 3. Kill running task with a third-party app; <br> <br> Elevate service priority <br> <br> Service boot <br> <br> 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: <br> <br> First Step: first create a broadcast receiver, refactor its abstract method OnReceive (context context, Intent Intent), in which you start the service or app you want to Start. <br> <br> </span> </span> </span></span></span></span></span></p> <ol> <ol> <li>Import android.content.BroadcastReceiver;</li> <li>Import android.content.Context;</li> <li>Import android.content.Intent;</li> <li>Import android.util.Log;</li> <li></li> <li></li> <li>public class Bootbroadcastreceiver extends Broadcastreceiver {</li> <li>Overriding the OnReceive method</li> <li>@Override</li> <li>public void OnReceive (context context, Intent Intent) {</li> <li>The xxx.class behind is the service to Start.</li> <li>Intent service = new Intent (context,xxxclass);</li> <li>Context.startservice (service);</li> <li>LOG.V ("TAG", "boot automatic service auto start ...");</li> <li>Launch the app with the package name of the app that needs to start automatically</li> <li>Intent Intent = Getpackagemanager (). getlaunchintentforpackage (packagename);</li> <li>Context.startactivity (intent);</li> <li>}</li> <li></li> <li></li> <li>}</li> </ol> </ol>Copy Code<p><p><span><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-size: small;"><br>Step two: Configure the XML file to receive this add Intent-filter configuration in Receiver<br><br></span></span></span></span></span></span></p></p> <ol> <ol> <li><receiver android:name= "bootbroadcastreceiver" ></li> <li><intent-filter></li> <li><action android:name= "android.intent.action.BOOT_COMPLETED" ></action></li> <li><category android:name= "android.intent.category.LAUNCHER"/></li> <li></intent-filter></li> <li></receiver></li> </ol> </ol>Copy Code<p><p><span><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-size: small;"><br>Step three: Add Permissions<br><br></span></span></span></span></span></span></p></p> <ol> <ol> <li><uses-permission android:name= "android.permission.RECEIVE_BOOT_COMPLETED"/></li> </ol> </ol>Copy Code<p><p><span><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-size: small;"><br>How to implement a process that will not be killed<br><br>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.<br><br>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.<br><br></span></span></span></span></span></span></p></p> <ol> <ol> <li>Add android:persistent= "true" into the <application> sections in your Androidmanifest.xml</li> </ol> </ol>Copy Code<p><span><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-family: Song body;"><span style="font-size:small;"> <br> remember, This is not an abuse, the system with this Service,app more, the whole system is Finished. <br> <br> There is a very limited number of phones in the system that must always be alive for trial Use. <br> <br> The method of raising the service priority <br> <br> 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. <br> <br> Why is foreground? the default service that is started is marked as background, and currently running Activity is generally marked as foreground, which means you set the service to Foregro Und then he has a certain increase in priority with the running Activity. When this doesn't guarantee that your Service will never be killed, it just increases his priority. <br> <br> Starting with Android 1.5, a service that has been started can call Startforeground (int, Notification) to set the service to the foreground State. Call Stopforeground (boolean) to set the service to the background State. &NBSP <br> <br> 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. <br> <br> in Android 1.0, set a service to foreground status: <br> <br> </span> </span> </span></span></span></span></span></p> <ol> <ol> <li>Setforeground (true);</li> <li>Mnm.notify (id, notification);</li> <li>Set a service to background state:</li> <li>Mnm.cancel (id);</li> <li>Setforeground (false);</li> </ol> </ol>Copy Code<p><p><span><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-size: small;"><br>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.<br><br></span></span></span></span></span></span></p></p> <ol> <ol> <li>Remote Service Controller & Binding</li> </ol> </ol>Copy Code<p><p><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-size: small;"><br>Call service across Processes. Not studied for the time Being.<br><br>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.<br><br>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.</span></span></span></span></span><br><br><span><span style="font-family: 微软雅黑, Verdana, sans-serif, 宋体;"><span><span style="font-family: 宋体;"><span style="font-size: small;">Original Link:</span></span> <span style="font-family: 宋体;"> <span style="font-size: small;">http://my.oschina.net/u/559701/blog/152341</span></span></span></span></span></p></p><p><p>How Android keeps the service from being killed-increasing process priority</p></p></span>
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