Android app --- WakeLock keeps the background awake

Source: Internet
Author: User

Some mobile apps (such as QQ) receive new messages. The screen of the mobile phone is on even when the screen is locked, prompting you to receive new messages. However, after the mobile phone lockscreen, the Android system will sleep after a period of time to save power and reduce CPU consumption, in the Android system, the CPU remains in a relatively low power consumption state, and a network request is bound to receive a new message, while a network request consumes CPU, so how can we keep the network status of the system and wake up the mobile phone through the program after the screen lock status and even the system enters sleep? The answer is the WakeLock mechanism in Android.


Official explanations for WakeLock:
PowerManager: This class gives you control of the power state of the device.
PowerManager. WakeLock: lets you say that you need to have the device on.

 

The Android system supports applications and services to save the program running status before standby, such as disabling file read/write, usb operations, and pausing music playing before standby. It also supports restoring the program status after waking up, such as restoring open files to read/write operations, restoring usb operations, and restoring music. The storage and recovery functions of these statuses ensure that the system works normally after the standby wake-up.

Two methods are provided:

1. Broadcast messages by standby mode and wake up broadcast messages.
2. Wakelock lock mechanism.

There are two parts to illustrate:

1. android system standby Processing Mechanism

Broadcast messages in standby mode and wake up broadcast messages
The system registers two broadcasts in the PowerManagerService class for sending before and after the standby mode.

Void initInThread (){
// After waking:
MScreenOnIntent = newIntent (Intent. ACTION_SCREEN_ON); // send after wakeup
MScreenOnIntent. addFlags (Intent. FLAG_RECEIVER_REGISTERED_ONLY );
// Before Standby:
MScreenOffIntent = newIntent (Intent. ACTION_SCREEN_OFF); // send when waiting
MScreenOffIntent. addFlags (Intent. FLAG_RECEIVER_REGISTERED_ONLY );
}

The following describes the priority of Broadcast Reception:
The receiver receives Intent sequentially according to the receiving sequence set in the Manifest. xml file. The receiving priority can be set in the system configuration file:
Declaration in the android: priority attribute of the intent-filter element, the greater the value, the higher the priority level, the value range is-1000 to 1000. Of course, you can also set it by calling the setPriority () method of the IntentFilter object.

Wakelock lock mechanism:
Applications can apply for the wakelock lock mechanism to vote on the system's standby mode. When any application applies for the wakelock lock, the system will not go into the standby mode, wait until the waelock lock of all applications is released.

2. Application Usage:

Instance code:


[Java] view plaincopyprint?
<SPAN style = "FONT-FAMILY: SimSun"> private WakeLock wakeLock = null;
 
/**
* Obtain the power lock to keep the service running when the screen is off and the CPU is still obtained
*/
Private void acquireWakeLock (){
If (null = wakeLock ){
PowerManager pm = (PowerManager) getSystemService (Context. POWER_SERVICE );
WakeLock = pm. newWakeLock (PowerManager. PARTIAL_WAKE_LOCK
| PowerManager. ON_AFTER_RELEASE, getClass ()
. GetCanonicalName ());
If (null! = WakeLock ){
Log. I (TAG, "call acquireWakeLock ");
WakeLock. acquire ();
}
}
}
 
// Release the power lock of the device
Private void releaseWakeLock (){
If (null! = WakeLock & wakeLock. isHeld ()){
Log. I (TAG, "call releaseWakeLock ");
WakeLock. release ();
WakeLock = null;
}
} </SPAN>

Private WakeLock wakeLock = null;

/**
* Obtain the power lock to keep the service running when the screen is off and the CPU is still obtained
*/
Private void acquireWakeLock (){
If (null = wakeLock ){
PowerManager pm = (PowerManager) getSystemService (Context. POWER_SERVICE );
WakeLock = pm. newWakeLock (PowerManager. PARTIAL_WAKE_LOCK
| PowerManager. ON_AFTER_RELEASE, getClass ()
. GetCanonicalName ());
If (null! = WakeLock ){
Log. I (TAG, "call acquireWakeLock ");
WakeLock. acquire ();
}
}
}

// Release the power lock of the device
Private void releaseWakeLock (){
If (null! = WakeLock & wakeLock. isHeld ()){
Log. I (TAG, "call releaseWakeLock ");
WakeLock. release ();
WakeLock = null;
}
}
WakeLock type and description:

PARTIAL_WAKE_LOCK: to keep the CPU running, the screen and keyboard lights may be disabled.
SCREEN_DIM_WAKE_LOCK: keeps the CPU running, allows screen display, but may be gray, allows the keyboard light to be turned off
SCREEN_BRIGHT_WAKE_LOCK: keeps the CPU running, allows screen highlighting, and allows you to turn off the keyboard light
FULL_WAKE_LOCK: keeps the CPU running, keeps the screen highlighted, and keeps the keyboard light bright.
ACQUIRE_CAUSES_WAKEUP: force the screen to light up. This lock mainly applies to operations that must be notified to the user.
ON_AFTER_RELEASE: When the lock is released, keep the screen on for a period of time


Finally, AndroidManifest. xml declares the permission:
<Uses-permission android: name = "android. permission. WAKE_LOCK"/>
<Uses-permission android: name = "android. permission. DEVICE_POWER"/>


If you want to save the data status before the standby mode, make sure that the standby mode is not enabled during this process. You can apply for the wakelock lock in onResume () or onStart () to call the acquireWakeLock () method.

In onPause () or onDistroy (), after processing the application STANDBY state, release the wakelock lock. In this case, call the releaseWakeLock () method.

 


Note the following:

In addition, the settings of WakeLock are Activiy-level, not for the entire Application. Therefore, you must note that there are multiple activities under the application!

 

 

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.