Android dual-process Service is resident in the background, without fear of "one-click cleaning", androidservice

Source: Internet
Author: User

Android dual-process Service is resident in the background, without fear of "one-click cleaning", androidservice
This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

Recently, services are used in the background. I have studied and found that both Q and Q use dual-process to ensure automatic resurrection after one-click cleaning. copy the example of Dual-process Service on the Internet, combined with onTrimMemory (), the system automatically recovers after one-click cleaning.

When using a dual-process Service, the key is to addAndroid: process = ": service1":

<service android:enabled="true" android:name="com.service.demo.Service1" android:process=":service1"></service><service android:enabled="true" android:name="com.service.demo.Service2" android:process=":service2"></service>        

The dual-process Service allows two processes to protect each other. After one of the services is cleaned up, other processes that are not cleaned can immediately restart the process.

-------- The following onTrimMemory explanations are referenced in the Network

OnTrimMemory () is an API provided after Android 4.0. The system calls back and forth based on the memory status. Responds to different memory release policies based on different memory statuses. The OnTrimMemory parameter is an int value, representing different memory states:

TRIM_MEMORY_COMPLETE: the memory is insufficient, and the process is located at the end of the background process list and will be cleared immediately.
TRIM_MEMORY_MODERATE: the memory is insufficient and the process is in the middle of the background process list.
TRIM_MEMORY_BACKGROUND: the memory is insufficient and the process is a background process.
TRIM_MEMORY_UI_HIDDEN: the memory is insufficient and the UI of the process is invisible.
The four above are 4.0 more
TRIM_MEMORY_RUNNING_CRITICAL
TRIM_MEMORY_RUNNING_LOW: insufficient memory (less than five background processes), and the priority of this process is relatively high, you need to clear the memory
TRIM_MEMORY_RUNNING_MODERATE: the memory is insufficient (more than five background processes), and the priority of the process is relatively high. The memory needs to be cleared.
The preceding three values increase by 4.1.

The example source code of this article can be downloaded here http://pan.baidu.com/s/1qW3KvtM

The following is the result of running the demo in this article: after the Service is enabled, the dual-process Service is started separately (Toast is displayed), and then the "one-click acceleration" is used to clean up the memory, the two-process Service is cleared one by one (the onTrimMemory () of the Service is triggered), but it is restarted later.



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.