Analysis of Android powermanager and powermanager. wakelock usage

Source: Internet
Author: User
Android powermanager and powermanager. wakelock usage analysis a previous Community PDA project needs to keep the screen always bright when collecting user coordinates. In the face of things not involved, the network is the best learning tool, find the relevant information and find that powermanager can be used. wakelock is used to implement the required functions. The following text is reprinted from a blog on the Internet. I feel that I have written it clearly. I can learn it and use it to write it to the project.

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 as follows:
Activiy level, not for the entire application.

The following is a simple piece of code used to wake up the screen:

 

[Java]
View plaincopyprint?
  1. Package com. eyu. wake_lock;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. OS. powermanager;
  5. Import Android. OS. powermanager. wakelock;
  6. Public class wake_lockactivity extends activity {
  7. Private Boolean iswakelock = true; // whether it is always on
  8. Private wakelock;
  9. /** Called when the activity is first created .*/
  10. @ Override
  11. Public void oncreate (bundle savedinstancestate ){
  12. Super. oncreate (savedinstancestate );
  13. Setcontentview (R. layout. Main );
  14. }
  15. @ Override
  16. Protected void onresume (){
  17. // Todo auto-generated method stub
  18. Powermanager PM = (powermanager) getsystemservice (power_service );
  19. Wakelock = PM. newwakelock (powermanager. screen_bright_wake_lock
  20. | Powermanager. on_after_release, "DPA ");
  21. If (iswakelock ){
  22. Wakelock. Acquire ();
  23. }
  24. Super. onresume ();
  25. }
  26. @ Override
  27. Protected void onpause (){
  28. // Todo auto-generated method stub
  29. Super. ondestroy ();
  30. If (wakelock! = NULL ){
  31. Wakelock. Release ();
  32. }
  33. Android. OS. process. killprocess (Android. OS. process. mypid ());
  34. }
  35. }
Package COM. eyu. wake_lock; import android. app. activity; import android. OS. bundle; import android. OS. powermanager; import android. OS. powermanager. wakelock; public class wake_lockactivity extends activity {private Boolean iswakelock = true; // whether the private wakelock is always on;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (SA Vedinstancestate); setcontentview (R. layout. main) ;}@ override protected void onresume () {// todo auto-generated method stub powermanager PM = (powermanager) getsystemservice (power_service); wakelock = PM. newwakelock (powermanager. screen_bright_wake_lock | powermanager. on_after_release, "DPA"); If (iswakelock) {wakelock. acquire ();} super. onresume () ;}@ override protected void onpause () {// todo Auto-generated method stub super. ondestroy (); If (wakelock! = NULL) {wakelock. Release ();} Android. OS. process. killprocess (Android. OS. process. mypid ());}}

In the onrusume method, the obtained lock is usually kept awake using the acquire () method. In the onpause method, the release () method is used to release the lock, use the lifecycle of the activity to skillfully pair the two methods.

 

 

Procedure for powermanager and wakelock
  1. Powermanager PM = (powermanager) getsystemservice (context. power_service );Context.getSystemService()Method to obtain the powermanager instance.
  2. Then, use powermanager's newwakelock (int
    Flags, string tag) to generate a wakelock instance. Int flags indicates the type of wakelock to be obtained. Different locks have different CPU
    , Screen, keyboard light has different effects.
  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 ).
The impact of int flags lock types on the CPU, screen, and keyboard:

Partial_wake_lock: Keep the CPU
The screen and keyboard lights may be turned off.

Screen_dim_wake_lock: Keep the CPU
Operation: allows the screen to be displayed, but may be gray. allows the keyboard light to be turned off.

Screen_bright_wake_lock: Keep the CPU
Operation: allows you to highlight the screen and turn off the keyboard light.

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

Acquire_causes_wakeup: the normal wake-up lock does not actually enable illumination. On the contrary, Once opened, they will remain (such as the activity of users in the afterlife ). When a wakelock is obtained, this flag immediately opens the screen or/and keyboard. A typical use is to immediately see notifications that are important to users.

On_after_release: this flag is set. When wakelock is released, the user activity timer is reset, resulting in illumination lasting for a period of time. If you loop in the wacklock condition, this can be used to reduce flickering

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.