There are two ways to implement this function. One is to declare in the manifest. xml file, and the other is to modify the flag of layoutparams in the code. The details are as follows:
1. Use user-Permission declaration in the manifest. xml file. The Code is as follows:
In this way, when installing the APK, the system will prompt the installer whether to allow the disable sleep function.
<Uses-Permission Android: Name = "android. Permission. wake_lock"/>
In order not to affect other programs, the code that enables and disables the normally highlighted screen is usually placed in the onresume () and onpause () events of the activity.
Powermanager PM = (powermanager) getsystemservice (context. power_service); // mytag can be written at will, and application name and other powermanager can be written. wakelock WL = PM. newwakelock (powermanager. screen_dim_wake_lock, "mytag"); // the screen is always on before it is released (it may be dimmed, but the screen content can also be seen and changed to powermanager. screen_bright_wake_lock won't darken) WL. acquire (); WL. release ();
2. Use code in the program. The Code is as follows:
Add this code before setcontentview (R. layout. Main). In this method, the installer is not prompted to disable sleep.
Getwindow (). setflags (windowmanager. layoutparams. flag_keep_screen_on, windowmanager. layoutparams. flag_keep_screen_on );