Mobile application development, often need to keep the phone screen constantly lit, or when the screen is locked, unlocked.
Android System (of course I developed the Android system version is 4.0) through the context also have translated to content, get the Window object, then you can set the current screen parameters.
The management of the screen lights, need to use the PowerManager object, through the system service to obtain the object. and the specific operation needs Android.os.PowerManager.WakeLock object to operate,
Look at the actual example. After execution, the resource of the operand needs to be freed. Attached Newwakelock Gets the parameter interpretation table for the action object.
| Flag Value |
CPU |
| Screen
Keyboard |
| Partial_wake_lock |
on* |
Off |
Off |
| Screen_dim_wake_lock |
On |
Dim |
Off |
| Screen_bright_wake_lock |
On |
Bright |
Off |
| Full_wake_lock |
On |
Bright |
Bright |
Of course, for PowerManager to learn more, still need to look at official documents.
Instance code:
1 //Unlock the keyboard to keep the screen light on. Settings are valid before Setcontentview. 2 GetWindow (). Addflags (3 WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD4|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED5|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);6PowerManager pm =(PowerManager) Getsystemservice (context.power_service);7WL =Pm.newwakelock (Powermanager.acquire_causes_wakeup8| Powermanager.screen_bright_wake_lock, "Alarmactivity");9Wl.acquire ();
View Code
For the interpretation of parameters, it is recommended to look at the official API documentation, explaining the comparative authority.
You also need to add permissions in the Manifest.xml configuration file when you use it:
1 < android:name= "Android.permission.WAKE_LOCK"/> <!--screen light operation Rights--
2 < android:name= "Android.permission.DISABLE_KEYGUARD"/> <!--unlock permissions --
Resources:
Partly from the Internet. Thank you for sharing!
Introduction to Android Development-------screen light, keypad lock