Calling a hidden API in Android to turn off the screen

I am thinking of using a hidden api to turn off the screen in my application.
setScreenState from https://android.googlesource.com/platform/frameworks/base/+/eclair-release/core/java/android/os/Power.java does what I want, but its hidden API. Does this mean that I should not use it? I would think this is a pretty stable API.
I am currently setting the screen timeout to 1 millisecond, and then reset the timeout after turning off the screen. However, the android ignores 1 millisecond, and instead takes about 3 seconds to turn off, and sometimes it completely ignores it and does not turn off.
Any suggestions?

+13
android
Dec 09 '09 at 17:55
source share
3 answers

Here's what I did to get around the need to make a sleep screen. You can do this in the activity window. I paired it, reducing the wait timeout to 5 seconds for this lockscreen user activity. You can view my entire source on my project page, but here is the relevant part about turning off the screen that worked for me on the droid.

 public void setBright(float value) { Window mywindow = getWindow(); WindowManager.LayoutParams lp = mywindow.getAttributes(); lp.screenBrightness = value; mywindow.setAttributes(lp); } //call this task to turn off the screen in a fadeout. class Task implements Runnable { public void run() { if (bright != 0) { setBright(bright/100); //start at 10% bright and go to 0 (screen off) bright--; serviceHandler.postDelayed(myTask, 100L); } else { setBright((float) 0.0); bright = 10;//put bright back } } } 

I used the handler task as a test for the method, it worked when I called it from onBackPressed in the first assembly. Now I just got setBright set to 0.0 with onCreate. This makes it so that the screen does not actually turn on, even if my user wakes up the processor by accidentally pressing the volume key. When I want the screen to continue, I have a setBright event call raised with a value greater than 0 (1.0 means max bright). I am very lucky this works for my lockscreen user activity. I found that changing the literal brightness setting does not work like that, and will not turn off the screen.

check out my other source on my svn project http://code.google.com/p/mylockforandroid/source/checkout

How difficult, in your opinion, is asking the android team to add support to turn off the screen or determine if the screen should wake up by replacing the Lock pick, similar to how you can program an alternative Home Launcher application?

+11
Jan 12
source share
+2
Feb 03 2018-11-22T00:
source share

setScreenState ... does what I want, but its hidden API. Does this mean that I should not use it?

Yes, that means you should not use it. In this case, the entire class will be excluded from the SDK. Please stick to the SDK .

+1
Dec 09 '09 at 19:50
source share



All Articles