简体   繁体   English

Android使用窗口标志关闭屏幕

[英]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. 我也尝试过使用WakeLocker。 The activity is being called from a BroadcastReceiver triggered from an alarm with simply 该活动是通过从广播触发的BroadcastReceiver调用的,只需简单地

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. 尝试将android:keepScreenOn =“ true”添加到此活动的布局XML资源中的某些UI小部件。 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. 根据其他答案的建议,WakeLock在技术上将起作用。 But then you have to manually release the WakeLock (if you mess that up, the screen will stay on a long time). 但随后您必须手动释放WakeLock(如果您将其弄乱了,屏幕将保持很长时间)。 And, since you could mess it up, you need the WAKE_LOCK permission. 并且,由于您可能将其弄乱,因此需要WAKE_LOCK权限。 Using keepScreenOn avoids all of that. 使用keepScreenOn可以避免所有这些情况。

Hope it works for you. 希望对你有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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