Android 以编程方式打开屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2891337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Turning on screen programmatically
提问by Matroska
I would like to unlock screen and switching it on to show a popup on an event trigger. I am able to unlock the screen using
我想解锁屏幕并将其打开以在事件触发器上显示弹出窗口。我可以使用解锁屏幕
newKeyguardLock = km.newKeyguardLock(HANDSFREE);
newKeyguardLock.disableKeyguard();
on KeyGuardService
but I cannot turn on the screen. I am using
打开KeyGuardService
但我无法打开屏幕。我在用
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, HANDSFREE);
wl.acquire();
but with no success. The screen still remains off. How can I achieve this?
但没有成功。屏幕仍然关闭。我怎样才能做到这一点?
回答by undefined
Note from author:I wrote this back in 2012. I don't know if it works anymore. Be sure to check out the other more recent answers.
作者注:我在 2012 年写了这个。我不知道它是否有效。请务必查看其他最近的答案。
Amir's answer got me close, but you need the ACQUIRE_CAUSES_WAKEUP
flag at least (Building against Android 2.3.3).
Amir 的回答让我很接近,但你ACQUIRE_CAUSES_WAKEUP
至少需要这个标志(针对 Android 2.3.3 构建)。
WakeLock screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
screenLock.acquire();
//later
screenLock.release();
回答by Vikasdeep Singh
This is very popular question but the accepted answer now is outdated.
这是一个非常受欢迎的问题,但现在接受的答案已经过时了。
Below is latest way to Turn On ScreenOR wake up your device screen from an activity:
以下是打开屏幕或从活动中唤醒设备屏幕的最新方法:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
this.setTurnScreenOn(true);
} else {
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
Use WindowManager.LayoutParams.FLAG_TURN_SCREEN_ONbut FLAG_TURN_SCREEN_ON flag has been deprecated in API level 27
so you can use Activity.setTurnScreenOn(true)from API level 27
onward.
使用WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON但 FLAG_TURN_SCREEN_ON 标志已被弃用,API level 27
因此您可以从以后使用Activity.setTurnScreenOn(true)API level 27
。
回答by Amir Dashti
In your main activity's OnCreate() write following code:
在您的主要活动的 OnCreate() 中编写以下代码:
((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG").acquire();
It causes the device to wake up.
它会导致设备唤醒。
Do not forget disableKeyguard() to unlock the device...
不要忘记 disableKeyguard() 来解锁设备...
回答by hmir
undefined's answer with NullPointer check and set timeout :
未定义的答案与 NullPointer 检查并设置超时:
private void turnOnScreen() {
PowerManager.WakeLock screenLock = null;
if ((getSystemService(POWER_SERVICE)) != null) {
screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
screenLock.acquire(10*60*1000L /*10 minutes*/);
screenLock.release();
}
}
回答by ZhengZhiren
I have the same issue. As these guys discussed here, there is a hidden api to turn screen on/off, see: https://android.googlesource.com/platform/frameworks/base/+/froyo-release/core/java/android/os/Power.java
我有同样的问题。正如这些人在这里讨论的那样,有一个隐藏的 api 可以打开/关闭屏幕,请参阅:https: //android.googlesource.com/platform/frameworks/base/+/froyo-release/core/java/android/os/电源.java
But I don't know how to call it. I have actually seen a application can turn screen on, wondering if it is device specific.
但我不知道如何称呼它。我实际上看到一个应用程序可以打开屏幕,想知道它是否特定于设备。