Android about improving service priorities for third-party apps

Source: Internet
Author: User

This blog as long as not marked "turn", then are original, paste please indicate this blog link link


Basically everyone knows that increasing the service priority can largely keep your service from being killed because of insufficient memory, but the system simply kills the lower priority at this point, and if the memory is not enough, it will kill your service. But now the machine is not like a few years ago, it basically does not happen that situation.


Let's take a look at the common error methods on the Web:


1.android:persistent= "true"

Not valid for third party apps, here's the official note

android:persistent
Whether or not the application should remain running @ all times-" true " if it should, and " false " if not. The default value is " false ". Applications should not normally set this flag; Persistence mode is a intended only for certain system applications.

Restart Service in 2.onDestroy

service is not necessarily executed when killed by the system OnDestroy , take what reboot

3.android:priority

Service does not have this attribute at all

4.setForeground

This is valid, but the online example is invalid because of a parameter error.

The way to keep the service from being criticized is to increase its importance, which has been stated in the official documentation that there are five levels in the process, and that the foreground process is the most important, so it is eventually killed.

How to turn it into a foreground process can be found in the official documentation.

Http://developer.android.com/guide/components/processes-and-threads.html

http://su1216.iteye.com/blog/1591699

This is just how to use Setforeground to set the service as the foreground process.

Notification Notification = new Notification (); notification.flags = notification.flag_ongoing_event; Notification.flags |= notification.flag_no_clear;notification.flags |= notification.flag_foreground_service; Service.startforeground (1, notification);
The above three attributes are put together with a value of 0x62.
    /** * Bit to was bitwise-ored into the {@link #flags} field that should being * Set if this notification are in ref  Erence to something that's ongoing, * like a phone call.     It should not being set if this notification are in the * reference to something the happened at a particular point in time,     * Like a missed phone call.    */public static final int flag_ongoing_event = 0x00000002; /** * Bit to is bitwise-ored into the {@link #flags} field This should be * set if the notification should isn't be     Canceled when the user clicks * The Clear All button.    */public static final int flag_no_clear = 0x00000020; /** * Bit to is bitwise-ored into the {@link #flags} field that should is * set if this notification represents a  Currently running service.     This * would normally is set for a by {@link service#startforeground}. */public static final int flag_foreground_service = 0x00000040;
Finally, we can use the following command to see which apps in your phone are doing this, and whether you're using them the longest and least likely to be killed by the system.
Dumpsys notification

Please keep the following link

My blog Address

http://su1216.iteye.com/

http://blog.csdn.net/su1216/

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.