When you need your device to run for a long time, due to the mobile device in order to prolong battery life, after running 15s-30mins (the user is free to set), if the user does not operate during this time period, the system will go into hibernation and
The screen is locked, so we need a way to keep the screen up and running in the long run, so we have this article. The following three methods are transferred from the network, the first method of pro-test can be effective, the rest of the method also ask the reader to test themselves.
The first method is when the activity is created,
Through "GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);" To keep the screen highlighted
(through "GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);" To dismiss the screen highlighting)
Of course, because you have this activity and screen highlighting to make a connection, when you jump to other activity will still walk the system settings.
This article turns from: "Android" disables lock screen and remains highlighted
There are several ways to implement "Disable lock screen, keep highlighting" on your code:
1. Add code to OnCreate () in each activity:
1 @Override2 protected voidonCreate (Bundle savedinstancestate) {3 Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.controller);5GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//Keep the screen highlighted while the app is running, without locking the screen6 findviews ();7 setlisteners ();8 init ();9}
2. Add code to Onresume and OnPause in each activity:
1 //enabled in Onresume2WakeLock =( (PowerManager) Getsystemservice (power_service))3 . Newwakelock (Powermanager.screen_bright_wake_lock4|powermanager.on_after_release, TAG);5 Wakelock.acquire ();6 7 //Disabled in OnPause8 if(WakeLock! =NULL) {9 wakelock.release ();Ten}
In the 2nd method, the permission needs to be added:
<uses-permission android:name= "Android.permission.WAKE_LOCK"/>
3. Add code to each activity:
1Mcontentresolver =getcontentresolver ();2Setlockpatternenabled (false);3 Public voidSetlockpatternenabled (Booleanenabled) {4 SetBoolean (Android.provider.Settings.System.LOCK_PATTERN_ENABLED,5 enabled);6 }7 Private voidSetBoolean (String Systemsettingkey,Booleanenabled) {8 android.provider.Settings.System.putInt (Mcontentresolver,9Systemsettingkey, enabled? 1:0);Ten}
In the 3rd method, the permission needs to be added:
<uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>
17, how to prevent screen hibernation Android device (reprint)