The PowerManager class is used to manage the power state of the device, the Powermanager.wakelock class is used to keep the device solid, and the power is controlled by various lock locks in Android, and it is important to note that the lock and unlock must appear in pairs.
Use the activity's life cycle to invoke keep the screen solid and free the screen-solid action.
/** * < function description > Keep the screen solid * * @return void [return type description] * /private void Keepscreenwake () { // Get Wakelock Lock, keep the screen solid Mpowermanager = (powermanager) getsystemservice (context.power_service); Mpowermanager.wakeup (Systemclock.uptimemillis ()); Mwakelock = Mpowermanager.newwakelock (Powermanager.full_wake_lock, this . GetClass (). Getcanonicalname ()); Mwakelock.acquire (); }
Accordingly, you need to release the on-screen operation in the activity's ondestory ().
/** * < function description > Release wakelock * * @return void [return type description] * /private void Releasewakelock () { if (Mwakelock! = null && Mwakelock.isheld ()) { mwakelock.release (); Mwakelock = null; } }
For instructions on the various flags, these flags primarily affect the CPU, screen, and keyboard:
Partial_wake_lock: Keep the CPU running, the screen and keyboard lights may be off;
Screen_dim_wake_lock: Keep the CPU running, run keep the screen display but may be gray, run off the keyboard light;
Screen_bright_wake_lock: Keep the CPU running, run to keep the screen highlighted, run off the keyboard lights;
Full_wake_lock: Keep the CPU running, keep the screen highlighted, and the keyboard lights remain highlighted;
For the above power management settings, you need to obtain the relevant permissions of the system:
<uses-permission android:name= "Android.permission.DEVICE_POWER"/><uses-permission android:name= " Android.permission.WAKE_LOCK "/>
Android:powermanager Class Power Management