唤醒 Android 设备
嘿,我需要在某个时间唤醒我正在睡觉的 Android 设备。 有什么建议吗?
PS 唤醒:打开显示屏并可能解锁手机
Hey i need to wake my sleeping android device up at a certain time.
Any suggestions?
P.S. Wake up: turn display on and maybe unlock phone
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
唤醒屏幕:
释放屏幕锁定:
并且清单文件需要包含:
有关 PowerManager 的更多详细信息,请参阅 API 文档: http://developer.android.com/reference/android/os/PowerManager.html
编辑:此答案已被报告为已弃用。
To wake up the screen:
To release the screen lock:
And the manifest needs to contain:
For more details about PowerManager, refer to the API documentation: http://developer.android.com/reference/android/os/PowerManager.html
EDIT: this answer is reported as deprecated.
最好是使用这些窗口标志的适当组合:
http:// /developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
http://developer.android.com/reference/android/view /WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
http://developer.android.com/reference/android/view /WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON
http://developer.android.com/reference/android/view /WindowManager.LayoutParams.html#FLAG_TURN_SCREEN_ON
如果您想在不支持所需标志的旧版本平台上运行,您可以直接使用唤醒锁和键盘锁...但是该路径充满了危险。
一个重要注意事项:您的活动必须全屏才能使上述标志组合发挥作用。在我的应用程序中,我尝试将这些标志与非全屏(对话框主题)的活动一起使用,但它不起作用。查看文档后,我发现这些标志要求窗口是全屏窗口。
Best is to use some appropriate combination of these window flags:
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_TURN_SCREEN_ON
If you want to run on older versions of the platform that don't support the desired flag(s), you can directly use wake locks and keyguard locks... but that path is fraught with peril.
ONE IMPORTANT NOTE: Your activity must be full screen in order for the above flag combination to work. In my app I tried to use these flags with an activity which is not full screen (Dialog Theme) and it didn't work. After looking at the documentation I found that these flags require the window to be a full screen window.
我找到了一种方法,它并不那么复杂......适用于任何 API 版本。
您需要使用
PowerManager.userActivity(l, false)
方法并将您的活动注册为 SCREEN_OFF 意图收到的广播:在您的活动 OnCreate 中输入类似以下内容:
它将在 Screen 2 秒后启动处理程序关闭事件。
在 onResume() 方法中注册接收器:
创建一个如下所示的处理程序:
在清单文件中请求权限:
完成后,不要忘记取消注册广播接收器。例如,您可以在 onDestroy() 中执行此操作(不能保证)
I found a way and it is not that complex... works on any API version.
You need to use
PowerManager.userActivity(l, false)
method and register your activity as broadcast received for SCREEN_OFF intent:In your actiivity OnCreate put something like:
It will kick off the handler after 2 seconds of Screen Off event.
Register receiver in your onResume() method:
Create a handler like the one below:
Request permission in your manifest file:
Do not forget to unregister broadcast receiver when you are done. You may do that in onDestroy() for example (which is not guaranteed)
在较新的设备上,您应该使用类似的东西,因为提到的标志已被弃用。
如果之前调用过 setter
setTurnScreenOn(true)
,则KeyguardManager.requestDismissKeyguard
仅唤醒设备。我在我的 Android Pie 设备上对此进行了测试。
On newer devices you should use something like this, since the mentioned Flags are deprecated.
KeyguardManager.requestDismissKeyguard
only wakes up the device, if the settersetTurnScreenOn(true)
was called before.I tested this on my Android Pie device.
在活动
onCreate()
方法中的setContentView(R.layout.YOUR_LAYOUT);
之后尝试使用以下代码Try with the below code after
setContentView(R.layout.YOUR_LAYOUT);
in activityonCreate()
method如果您在醒来时显示一个窗口,则可以通过向您的活动添加一些标志来轻松使其工作,而无需使用唤醒锁。
If you are showing a window when waking up, you can get it working easily by adding few flags to your activity, without using a wake lock.
以编程方式设置闹钟将唤醒手机(播放声音),我想打开显示将是那里的一个选项。
我认为不会有一个公开的 API 可以自动解锁手机。
Settling an alarm programatically will wake up the phone(play a sound) and i guess the turn on display would be an option there.
I donot think there would be an exposed API that will unlock the phone automatically.
将解除通用键盘保护并导致设备解锁。
will dismiss the general keyguard and cause the device to unlock.