Android powermanager and powermanager. wakelock

Source: Internet
Author: User
Preface I have been studying Android for a while. In order to further understand how Android applications are designed and developed, I decided to study several open-source Android applications in detail. From some open-source applications to absorb something, while accumulating the amount, while exploring Android learning and research directions. Here, I first selected the jwood standup timer project. This article will sort out the study content notes and create an index list. Powermanager. wakelock powermanager. wakerlock is my analysis standup Timer Source code It is a small knowledge point found, standup timer uses wakelock to ensure Program Keep the screen on the phone while running (the program is small but very careful, considerate ). Powermanager and powermanager. wakerlock7 are used to manage the power supply of Android devices. 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. in Android, the power supply is controlled through various lock locks. Note that the lock and unlock must appear in pairs. First Code Then describe it. Code

Private Void Acquirewakelock (){
If (Wakelock = Null ){
Logger. D ( " Acquiring wake lock " );
Powermanager pm = (Powermanager) getsystemservice (context. power_service );
Wakelock = PM. newwakelock (powermanager. screen_dim_wake_lock, This . Getclass (). getcanonicalname ());
Wakelock. Acquire ();
}

}

PrivateVoidReleasewakelock (){
If(Wakelock! =Null&&Wakelock. isheld ()){
Wakelock. Release ();
Wakelock=Null;
}

}

 

The screen_dim_wake_lock lock is obtained in the acquirewakelock () method, which keeps the CPU running and the screen brightness (which can be dimmed ). This function is called in onresume of activity. The releasewakelock () method is to release the lock. It is called in onpause of activity. The Activiy life cycle is used to skillfully present acquire () and release () in pairs.

@ Override
Protected Void Onresume ()
{
Super . Onresume ();
// Get the lock to keep the screen brightness
Acquirewakelock ();
Starttimer ();
} Code

Protected Void Onpause ()
{
Super . Onpause ();
Synchronized ( This ){
Canceltimer ();
Releasewakelock ();

If(Finished ){
Clearstate ();
}Else{
Savestate ();
}
}
}

 

Procedure for powermanager and wakelock
    1. Powermanager PM = (powermanager) getsystemservice (context. power_service );Context. getsystemservice ()Method to obtain the powermanager instance.
    2. ThenNewwakelock(INT flags, string tag) to generate a wakelock instance. Int flags indicates the type of wakelock to be obtained. Different locks have different effects on CPU, screen, and keyboard lights.
    3. After obtaining the wakelock instance, obtain the corresponding lock through acquire (), perform other business logic operations, and finally use release () to release (release is required ).
About int flags The impact of various lock types on the CPU, screen, and keyboard:

Partial_wake_lock: Keep the CPU running. The screen and keyboard lights may be disabled.

Screen_dim_wake_lock: Keep the CPU running. It allows screen display but may be gray. You can turn off the keyboard light.

Screen_bright_wake_lock: Keep the CPU running, enable screen highlighting, and disable keyboard lights

Full_wake_lock: Keep the CPU running, screen highlighted, and keyboard light brightness

Acquire_causes_wakeup: Normal wake locks don't actually turn on the illumination. instead, they cause the illumination to remain on once it turns on (e.g. from user activity ). this flag will force the screen and/or keyboard to turn on immediately, when the wakelock is acquired. A typical use wocould be for communications which are important for the user to see immediately.

On_after_release: F this flag is set, the user activity timer will be reset when the wakelock is released, causing the illumination to remain on a bit longer. this can be used to reduce flicker if you are using ing between wake lock conditions.

You must declare in androidmanifest. XML that the application has the permission to set power management. < Uses-Permission Android: Name = "Android. Permission. wake_lock" />
You may need
< Uses-Permission Android: Name = "Android. Permission. device_power" />

In addition, the settings of wakelock are Activiy-level, not for the entire application.
Series Indexes
Android open-source project-standuptimer learning notes Index

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