简体   繁体   中英

Android turn screen off using Window Flags

I have been trying to turn off the users screen now for a while but with no luck. I have tried this:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

to turn the screen on and then

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

to turn the screen off. I have also tried to use a WakeLocker. The activity is being called from a BroadcastReceiver triggered from an alarm with simply

Intent i = new Intent(context.getApplicationContext(), WakeupActivity.class);
    context.startActivity(i);

I have tried many different solutions but cannot figure this out. Thanks

Try adding android:keepScreenOn="true" to some UI widget in your layout XML resource for this activity. So as long as, that widget is visible on the screen, the screen will not turn off automatically.

NOTE:

A WakeLock, as suggested by other answers, technically will work. But then you have to manually release the WakeLock (if you mess that up, the screen will stay on a long time). And, since you could mess it up, you need the WAKE_LOCK permission. Using keepScreenOn avoids all of that.

Hope it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM