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?
- 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 it is always on
- Private wakelock;
- /** Called when the activity is first created .*/
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- 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 ());
- }
- }
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
- Powermanager PM = (powermanager) getsystemservice (context. power_service );
Context.getSystemService()
Method to obtain the powermanager instance.
- 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.
- 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