在android中调用隐藏的API来关闭屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1875669/
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
Calling hidden API in android to turn screen off
提问by David Shellabarger
I'm thinking about using a hidden api to turn the screen off in my app.setScreenState
from https://android.googlesource.com/platform/frameworks/base/+/eclair-release/core/java/android/os/Power.javadoes what I want, but its a hidden API. Does that mean I shouldn't use it? I would think its a fairly stable API.
Currently, I'm setting the screen timeout to 1 millisecond, and then resetting the timeout once the screen turns off. However, android ignores the 1 millisecond and instead it takes about 3 seconds to turn off and sometimes it ignores it completely and doesn't turn off.
Any suggestions?
我正在考虑使用隐藏的 api 在我的应用程序中关闭屏幕。setScreenState
从https://android.googlesource.com/platform/frameworks/base/+/eclair-release/core/java/android/os/Power.java做我想要的,但它是一个隐藏的 API。这是否意味着我不应该使用它?我认为它是一个相当稳定的 API。
目前,我将屏幕超时设置为 1 毫秒,然后在屏幕关闭后重置超时。但是,android 忽略了 1 毫秒,而是需要大约 3 秒才能关闭,有时它会完全忽略它并且不会关闭。
有什么建议?
采纳答案by mylock
Here's what I did to work around the need to make the screen sleep. You can do this in an activity window. I paired it with reducing the sleep timeout to 5 sec for this custom lockscreen activity. You can view all my source over at my project page, but here's the relevant part about turning the screen off that worked for me on a droid.
这是我为解决使屏幕休眠的需要所做的工作。您可以在活动窗口中执行此操作。我将它与将此自定义锁屏活动的睡眠超时减少到 5 秒相结合。您可以在我的项目页面上查看我的所有源代码,但这里是有关关闭屏幕的相关部分,该部分在我的机器人上对我有用。
public void setBright(float value) {
Window mywindow = getWindow();
WindowManager.LayoutParams lp = mywindow.getAttributes();
lp.screenBrightness = value;
mywindow.setAttributes(lp);
}
//call this task to turn off the screen in a fadeout.
class Task implements Runnable {
public void run() {
if (bright != 0) {
setBright(bright/100); //start at 10% bright and go to 0 (screen off)
bright--;
serviceHandler.postDelayed(myTask, 100L);
} else {
setBright((float) 0.0);
bright = 10;//put bright back
}
}
}
I used the handler task as a test for the method, it worked when I called it from onBackPressed in the first build. Now, I just have the activity setBright to 0.0 at onCreate. This makes it so the screen doesn't actually turn on even if my user wakes the CPU by an accidental volume key press. When I want the screen to go on, I have the key event call setBright to a value greater than 0 (1.0 means max bright). I'm very lucky this works for my custom lockscreen activity. I found that changing the literal brightness system setting doesn't work like this, and won't get the screen off.
我使用处理程序任务作为该方法的测试,当我在第一次构建中从 onBackPressed 调用它时它起作用了。现在,我只是在 onCreate 将活动 setBright 设置为 0.0。这使得即使我的用户意外按下音量键唤醒了 CPU,屏幕也不会真正打开。当我希望屏幕继续时,我将键事件调用 setBright 设置为大于 0 的值(1.0 表示最大亮度)。我很幸运这适用于我的自定义锁屏活动。我发现更改文字亮度系统设置不会像这样工作,并且不会关闭屏幕。
check out my other source over on my project svn http://code.google.com/p/mylockforandroid/source/checkout
在我的项目 svn http://code.google.com/p/mylockforandroid/source/checkout上查看我的其他来源
How hard do you think it is to ask the android team to add support for turning the screen off or defining whether the screen should wake via Lock mediator replacement similarly to how you could program an alternative Home Launcher app?
您认为要求 android 团队添加对关闭屏幕的支持或定义屏幕是否应通过 Lock mediator 替换唤醒的支持与您如何编写替代 Home Launcher 应用程序类似,有多难?
回答by Lino Barreca
回答by CommonsWare
setScreenState...does what I want, but its a hidden API. Does that mean I shouldn't use it?
setScreenState...做我想要的,但它是一个隐藏的 API。这是否意味着我不应该使用它?
Yes, it means you shouldn't use it. In this case, the whole class would appear to be excluded from the SDK. Please stick to the SDK.
是的,这意味着你不应该使用它。在这种情况下,整个类似乎都被排除在 SDK 之外。请坚持使用 SDK。