Android improves the service priority of third-party apps

Source: Internet
Author: User

This blog is original as long as it does not indicate "Conversion". For the post, please indicate the link to this blog.

Basically, we all know that improving the service priority can greatly protect your service from being killed due to insufficient memory. Of course, the system only kills the lower-priority kill first, if the memory is not enough, your service will be killed. However, the current machine is not like a few years ago, basically it will not happen that way.

Let's take a look at the common onlineErrorMethod:

1. android: persistent = "true"

Invalid for third-party apps. The following is an official description

android:persistentWhether or not the application shocould remain running at all times -" true"If it shoshould, and" false"If not. The default value is" false". Applications shocould not normally set this flag; persistence mode is intended only for certain system applications.

2. Restart service in onDestroy

When the service is killed by the system, it does not necessarily execute onDestroy.

3. android: priority

The service does not have this attribute at all.

4. setForeground

This is valid, but the online examples are invalid because of parameter errors.

The way to prevent the service from being difficult is to increase its importance. In the official documentation, there are five levels of processes. The front-end processes are the most important, so they are finally killed.

For details about how to turn it into a foreground process, refer to the official documentation.



This document only describes how to use setForeground to set the service as a 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);
Put the preceding three attributes together, with a value of 0x62.
    /**     * Bit to be bitwise-ored into the {@link #flags} field that should be     * set if this notification is in reference to something that is ongoing,     * like a phone call.  It should not be set if this notification is in     * reference to something that happened at a particular point in time,     * like a missed phone call.     */    public static final int FLAG_ONGOING_EVENT      = 0x00000002;    /**     * Bit to be bitwise-ored into the {@link #flags} field that should be     * set if the notification should not be canceled when the user clicks     * the Clear all button.     */    public static final int FLAG_NO_CLEAR           = 0x00000020;    /**     * Bit to be bitwise-ored into the {@link #flags} field that should be     * set if this notification represents a currently running service.  This     * will normally be set for you by {@link Service#startForeground}.     */    public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
Finally, we can use the following command to check which applications on the mobile phone are doing this. Do you have the longest survival time and are most likely to be killed by the system during normal use?
dumpsys notification

Repost the following link

My blog address



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: 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.