Recently encountered a project, run up and found that the phone screen has always been bright, online search has two methods, but did not solve the problem
The first method:
Everyone think, we are in the Android that place always define the permission Ah, with this hint, we will think of which file it, Androidmanifest.xml: We have to define the permissions in this file can be realized our mobile phone screen is kept constantly lit. This is also the simplest method, so let's take a look at the code below:
Java code:
01.<uses-permission android:name= "Android.permission.WAKE_LOCK"/>
01.PowerManager pm = (powermanager) getsystemservice (Context.power_service);
02.powermanager.wakelock Mwakelock = Pm.newwakelock (Powermanager.screen_dim_wake_lock, "My Tag");
03.//in Onresume () call
04.
05.mwakelock.acquire ();
06.//in OnPause () call
07.mwakelock.release ();
We're going to write the Powermanager.screen_dim_wake_lock in the main code, this is what our Android system gives us, and we want to use it, and that code means it's long-lit, so we can do it, Because we have defined permissions on the above. This gives us the right to use this long light attribute. This is the first method.
The second method:
The second is not very common, but sometimes we can use it, so let's talk about this second method. In this way we will do a method in the main code, this method is OnCreate (Bundle icicle) We have to write the parameters in parentheses, so that we can use this parameter, we are in the super.oncreate (icicle); The meaning of this sentence is to get the parameters, we are finished, and finally we find layoutparams.flag_keep_screen_on this Android system provides us with the properties, so that our second method is done, This method does not have to define permissions in the Androidmanifest.xml:. This is not more trouble, but sometimes the parameters to forget, this is the focus, because so we can not achieve the effect, so to speak, two methods have advantages and disadvantages, with the time to look at the developer's own preferences, preferences which to use which.
Java code:
[Email protected]
02.protected void OnCreate (Bundle icicle) {
03.super.oncreate (Icicle);
04.
05.getWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
06.}
Neither of the above solves the problem of my screen solid not standby
in the tangle of a day more after I decided to write a demo to verify, the original project main code extracted, test, demo after the discovery or often bright, I began to suspect that is not the source of Android bug, after repeated tangled I found in the code so that a sentence, View.setkeepscreenon (True) deleted after the re-test, to 15 seconds after the screen to resume standby, an instant kind of want to die feeling , it is so human egg ache , explain Setkeepscreenon (true),
"Please keep the screen turned on for calibration. "True, there is the meaning of calibration." And does View.setkeepscreenon (true) require "Android.permission.WAKE_LOCK" permissions? My current inference is that "G1 (Cupcake 1.5) is not required , emulator and other devices/platforms may be required."
"Force close", you might as well try adding this permission. The problem with the egg is finally solved.
Keep your phone screen solid without permission