Android 强制开启屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2131948/
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
Force Screen On
提问by bugzy
How do I force the screen to stay active and not shut off while my app is running?
如何在我的应用程序运行时强制屏幕保持活动状态而不关闭?
回答by hackbod
PLEASE DO NOT USE A WAKE LOCK
请不要使用唤醒锁
This requires that you give your app an additional permission, and it is very easy to introduce bugs where you accidentally remain holding the wake lock and thus leave the screen on.
这需要你给你的应用一个额外的权限,很容易引入错误,你不小心保持唤醒锁定,从而使屏幕保持开启状态。
It is far, far better to use the window flag FLAG_KEEP_SCREEN_ON
, which you can enable on your activity's window in your onCreate()
like this:
使用 window 标志要好得多FLAG_KEEP_SCREEN_ON
,您可以onCreate()
像这样在您的活动窗口中启用它:
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
This will make sure that the screen stays on while your window is in the foreground, and only while it is in the foreground. It greatly simplifies this common use case, eliminating any juggling you need to do as your app transitions between states.
这将确保当您的窗口在前景中时屏幕保持开启,并且仅当它在前景中时。它极大地简化了这个常见用例,消除了您在应用程序状态之间转换时需要做的任何杂耍。
回答by Tarsem Singh
This Question has Already Great Answer by @hackbod !
这个问题已经由@hackbod 给出了很好的答案!
I am Answering this Question with Two Additional Solutions !
我用两个额外的解决方案来回答这个问题!
Existing Solution :
现有解决方案:
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Additional Solutions:
其他解决方案:
we can use keepScreenOn
我们可以用 keepScreenOn
1.implementation
using setKeepScreenOn() in java code
1.implementation
使用setKeepScreenOn() in java code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// or any View (in case generated programmatically )
View v = getLayoutInflater().inflate(R.layout.driver_home, null);
v.setKeepScreenOn(true);
setContentView(v);
}
Docs http://developer.android.com/reference/android/view/View.html#setKeepScreenOn(boolean)
文档http://developer.android.com/reference/android/view/View.html#setKeepScreenOn(boolean)
2.Adding keepScreenOn
to xml layout
2.添加keepScreenOn
到xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true" >
Docs http://developer.android.com/reference/android/view/View.html#attr_android%3akeepScreenOn
文档http://developer.android.com/reference/android/view/View.html#attr_android%3akeepScreenOn
Note ( Some Useful Points) :
注意(一些有用的点):
1.it Doesn't matter that keepScreenOn
should be used on Main/Root/Parent View
it can be used with any child view
will work As same as it works in Parent view
1.这不要紧,keepScreenOn
就应该使用Main/Root/Parent View
它可以与任何使用child view
将作为相同的,因为它在工作Parent view
2.The Thing Only matter is that View's Visibility must be visible
other wise it will not work !
2.唯一的事情是,View's Visibility must be visible
否则它不会起作用!
回答by Alexander Abramov
Another solution is to add android:keepScreenOn="true"
(documentation) to the views that need to keep the screen on.
另一种解决方案是将android:keepScreenOn="true"
(文档)添加到需要保持屏幕开启的视图中。
Allows for a little bit more granular control in terms of which views stay on and which don't. You can even reference a setting from a resource file this way.
允许对哪些视图保持开启和哪些不开启进行更精细的控制。您甚至可以通过这种方式从资源文件中引用设置。