Calling hidden API in android to turn screen off

I'm thinking about using a hidden api to turn the screen off in my app.
setScreenState from does what I want, but its a hidden API. Does that mean I shouldn't use it? I would think its a fairly stable API.
Currently, I'm setting the screen timeout to 1 millisecond, and then resetting the timeout once the screen turns off. However, android ignores the 1 millisecond and instead it takes about 3 seconds to turn off and sometimes it ignores it completely and doesn't turn off.
Any suggestions?

Asked by: Brad161 | Posted: 20-01-2022

Answer 1

Here's what I did to work around the need to make the screen sleep. You can do this in an activity window. I paired it with reducing the sleep timeout to 5 sec for this custom lockscreen activity. You can view all my source over at my project page, but here's the relevant part about turning the screen off that worked for me on a droid.

public void setBright(float value) {
    Window mywindow = getWindow();
    WindowManager.LayoutParams lp = mywindow.getAttributes();
    lp.screenBrightness = value;

//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)
            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 build. Now, I just have the activity setBright to 0.0 at onCreate. This makes it so the screen doesn't actually turn on even if my user wakes the CPU by an accidental volume key press. When I want the screen to go on, I have the key event call setBright to a value greater than 0 (1.0 means max bright). I'm very lucky this works for my custom lockscreen activity. I found that changing the literal brightness system setting doesn't work like this, and won't get the screen off.

check out my other source over on my project svn

How hard do you think it is to ask the android team to add support for turning the screen off or defining whether the screen should wake via Lock mediator replacement similarly to how you could program an alternative Home Launcher app?

Answered by: Alfred587 | Posted: 21-02-2022

Answer 2

setScreenState...does what I want, but its a hidden API. Does that mean I shouldn't use it?

Yes, it means you shouldn't use it. In this case, the whole class would appear to be excluded from the SDK. Please stick to the SDK.

Answered by: Lana162 | Posted: 21-02-2022

Answer 3

Use a partial wake lock

Answered by: Fiona432 | Posted: 21-02-2022

Similar questions

java - Calling different layers from the same class

I am here to seek some help with my code which i am facing a dead end road with. I'm trying to pass values from using Intent to Passing the values is fine and I managed to get through it; however, when I check using if statement the program crash down. Here are my files, plzzzzzzzzzzz help package; import; import android.cont...

android - Calling service method to widget

Hi I have a android widget, that show info from one service application that I have. I update the info from the service and this is running good. But I have a problem. My service works in second plane when the application is close, if I create the widget when the app is second plane the service is running and how my service is running when my widget try to start the services dont make (This is good) but this makes it that ...

android - Get calling context in a service

So I'm working on a service that will handle requests to send data to a socket. I've done socket programming in a service before, but only within the same application. I'd like this to just sit and do nothing until any application wants to give it data to send. That's all well and good. I have register an intent filter and the usual stuff and can process the data. But ...

Calling a Web Service in Android

I am currently working on an application in android where I need to call the Web Service. I am working in Android and I don't have an idea about how to call a web Service in Android. So, can anybody please help me out in how to call a Web Service in Android Programming. Thanks, david

Android ,Calling buttons from include tag

I followed @seanhodges sample of the footer bar (android navigation bar bottom) : Is it possible to call it from include tag and then access each button at runtime <LinearLayout android:layout_alignParentBottom="true" android:layout_height="wrap_content" a...

java - Thread Calling in Android

I am working on an Android Application where I need to work on thread which is a well known concept of Java. I am having a thread named "thrd" in my application and I want to call it in the else part of the IF...Else loop. Can anyone please tell me how can I call that thread in my loop, Sorry I am totally new as far as working with Java or Android is concerned. Thanks for the help in Advance, david

Calling the default media player with an URI on Android

I found this code: Uri u = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); i.setData(url); startActivity(i); That plays a sound with the default media player. I want to call the same media player with a URI that contains a URL. How can I target the default player?

Android: calling number without user init?

For something like a 'help I can't get up' app -- is there a way for a user to set a contact number, and then for the app to call that number in the future WITHOUT the user initiating the dialout? I've tried the below but there seems to be a problem: private void callPhone(){ if(phoneNumber.length()>0){ try { Intent intent = new Intent(Intent.ACTION_CA...

Calling Android intent from Web page?

i want to launch an intent from android website what code do i need to add in the HTML , for example how could i launch the dialer intent after the user click on number in the website . i saw some example in the web like when you click download app in website and it launches the android market . thanks

android - calling array in main class

I have two classes and one class contain string array and its constructor how to call it in main with its object?

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android