在 Android 上关闭屏幕
我试图在某个操作发生后打开和关闭显示器(我们现在只关心关闭屏幕)。根据我对唤醒锁的理解,这就是我所拥有的:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
当我在 stackoverflow 和其他地方阅读其他帖子时,他们似乎告诉我 PARTIAL_WAKE_LOCK 会关闭屏幕。但如果我读了 SDK,它说它只允许关闭屏幕。我认为这是不对的。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关闭屏幕有两种选择:
您可能也需要此权限:
更新:
尝试此方法;一旦光线足够低,android就会关闭屏幕。
There are two choices for turning the screen off:
You will probably need this permission too:
UPDATE:
Try this method; android turns off the screen once the light level is low enough.
以下内容复制自SDK 文档。
如果你想保持屏幕打开,我认为
SCREEN_BRIGHT_WAKE_LOCK
就足够了。The following is copied from SDK document.
If you want to keep screen on, I think
SCREEN_BRIGHT_WAKE_LOCK
is enough.对我来说这些方法不起作用。所以我使用了其他场景(并非微不足道)来关闭屏幕。
Android 有 2 个负责唤醒的标志:
我使用了以下流程:
首先保存您以前的配置,例如屏幕超时为 1 分钟,并选中充电时保持清醒。
之后,我取消选中充电时保持清醒并将屏幕超时设置为最短时间。
我注册到广播接收器服务以从屏幕关闭的 Android 获取事件。
当我关闭屏幕上的事件时,我将之前的配置设置为默认值:屏幕超时为 1 分钟,并选中充电时保持清醒。
取消注册接收器
15 秒后。设备睡眠
以下是代码片段:
BroadcastReceiver
用作回调的接口
主类中的 2 个方法:
AndroidSynchronize 类
< strong>[编辑]
您需要注册权限:
For me those methods didn't work. So I used other scenario (not trivial) to make my screen off.
Android has 2 flags that responsible to be awake:
I used followed flow:
1st of all save your previous configuration, for example screen timeout was 1 min and Stay awake while charging checked.
After, I uncheck Stay awake while charging and set screen timeout to minimal time.
I register to broadcast receiver service to get event from android that screen turned off.
When I got event on screen off, I set previous configuration to default: screen timeout was 1 min and Stay awake while charging checked.
Unregister receiver
After 15 sec. device sleeps
Here is snippets of code:
BroadcastReceiver
Interface used as callback
2 methods from main class:
AndroidSynchronize class
[EDIT]
You need to register permission:
根据此链接,您还可以转动屏幕像这样关闭:
1000 是以毫秒为单位,这意味着 1 秒,您可以根据需要将其替换为任何值。
需要许可:
Per this link, You can also turn the screen off like this:
1000 is in milliseconds which means 1 second, you can replace it with any value as desired.
Needed permission:
尝试 -
wakeLock.acquire(1000); //指定时间,它会变暗并最终关闭。
try -
wakeLock.acquire(1000); // specify the time , it dims out and eventually turns off.