Android: How to turn screen on and off programmatically?

后端 未结 16 2058
醉酒成梦
醉酒成梦 2020-11-22 13:05

Before marking this post as a \"duplicate\", I am writing this post because no other post holds the solution to the problem.

I am trying to turn off the device, then

相关标签:
16条回答
  • 2020-11-22 13:23

    Here is a successful example of an implementation of the same thing, on a device which supported lower screen brightness values (I tested on an Allwinner Chinese 7" tablet running API15).

    WindowManager.LayoutParams params = this.getWindow().getAttributes();
    
    /** Turn off: */
    params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
    //TODO Store original brightness value
    params.screenBrightness = 0.1f;
    this.getWindow().setAttributes(params);
    
    /** Turn on: */
    params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
    //TODO restoring from original value
    params.screenBrightness = 0.9f;
    this.getWindow().setAttributes(params);
    

    If someone else tries this out, pls comment below if it worked/didn't work and the device, Android API.

    0 讨论(0)
  • 2020-11-22 13:24

    If your app is a system app,you can use PowerManager.goToSleep() to turn screen off,you requires a special permission

    before you use goToSleep(), you need use reflection just like:

    public static void goToSleep(Context context) {
        PowerManager powerManager= (PowerManager)context.getSystemService(Context.POWER_SERVICE);
        try {
            powerManager.getClass().getMethod("goToSleep", new Class[]{long.class}).invoke(powerManager, SystemClock.uptimeMillis());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
    

    Now,you can use goToSleep() to turn screen off.

    This is what happens when the power key is pressed to turn off the screen.

    0 讨论(0)
  • 2020-11-22 13:27

    To keep screen on:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    

    Back to screen default mode: just clear the flag FLAG_KEEP_SCREEN_ON

    getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
    0 讨论(0)
  • 2020-11-22 13:31

    I wouldn't have hope of "waking the screen" in the activity. If the screen is off the activity is probably in a paused state and shouldn't be running any code.

    When waking up, there is the issue of the lockscreen. I don't know how any app can automatically bypass the lockscreen.

    You should consider running your background tasks in a service, and then using the notification manager to send a notification when whatever is detected. The notification should provide some sort of device alert (screen wake up, notification icon, notification led, etc). When clicking the notification it can launch the intent to start your activity.

    You could also attempt to start the activity direct from the service, but I really don't know if that will turn the screen on or bypass the lockscreen.

    0 讨论(0)
  • 2020-11-22 13:34

    I had written this method to turn on the screen after screen lock. It works perfectly for me. Try it-

        private void unlockScreen() {
            Window window = this.getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        }
    

    And call this method from onResume().

    0 讨论(0)
  • 2020-11-22 13:34

    Simply add

    android:keepScreenOn="true" 
    

    or call

    setKeepScreenOn(true) 
    

    on parent view.

    0 讨论(0)
提交回复
热议问题