在 Android 中通过摇动打开/关闭屏幕

发布于 2024-10-20 17:27:27 字数 571 浏览 2 评论 0原文

我正在制作一个应用程序,需要在用户摇动手机时打开/关闭屏幕。到目前为止,我有一个 SensorEventListener 可以按照 的答案中的建议监听震动这个问题

当我检测到震动时,我会按照

我想做的事情可能吗?我做错了什么?

-- 编辑 --

感谢 Dreicyerasor 我已经进一步研究了这个问题。在将亮度设置为0.0f,但是当我摇动手机时它仍然无法打开。然而,当我调试应用程序时,我发现当我在屏幕关闭的情况下摇动手机时,setBright(1.0f) 会被正确调用;我怀疑锁屏在某种程度上弄乱了它,因为当我按下电源按钮时它就会启动。按下电源按钮后,应用程序将继续像平常一样工作。有没有办法绕过锁屏?

感谢您的帮助!

I'm making an app that needs to toggle the screen on/off when the user shakes the phone. So far, I've got a SensorEventListener that listens to the shakes as suggested in the answer to this question.

When I detect a shake, I change the value of the screen's brightness as suggested in this question's answer. It all works great if I don't actually turn the screen off... if I set the brightness to 0.01f through the public void setBright(float value) method it works perfectly. However, if I set the brightness to 0.0f, the phone won't turn the screen again... not until I press the power button, at least.

Is what I'm trying to do possible? what am I doing wrong?

-- EDIT --

Thanks to Dre and icyerasor I've looked further into the issue. I acquire a PARTIAL_WAKE_LOCK before I set the brightness to 0.0f, but it still doesn't turn on when I shake the phone. However, when I debug the app I see that the setBright(1.0f) gets called allright when I shake the phone with the screen turned off; My suspicion is that the lockscreen is somehow messing with it, since it kicks in when I press the power button. After I press the power button, the app continues to work as it usually does. Is there a way to bypass the lockscreen?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘亓 2024-10-27 17:27:27

不过,我同意 icyerasor 的猜测——如果猜测正确并且手机将进入睡眠状态,您将必须获取 PARTIAL_WAKE_LOCK 在将亮度设置为 0.0 之前保持 CPU 唤醒

我会在回答之前对此进行测试,但是我目前无法访问 Android 设备。

I agree with icyerasor's guess, however -- If the guess is correct and the phone is going to sleep you will have to acquire a PARTIAL_WAKE_LOCK to keep the CPU awake before you set the brightness to 0.0

I would test this before answering but I don't have access to an Android device at this moment.

贪恋 2024-10-27 17:27:27

只是猜测:将其设置为 0.0 也可能使手机进入睡眠模式?

当您想以编程方式再次打开它时,请尝试获取 ACQUIRE_CAUSES_WAKEUP 唤醒锁:

PowerManager pm = (PowerManager)mContext.getSystemService(
                                          Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
                                      PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                                      | PowerManager.ACQUIRE_CAUSES_WAKEUP,
                                      TAG);
wl.acquire(1000);

Just a guess: Setting it to brighnes 0.0 might also put the phone in sleep mode?

When you want to turn it on again programmatically, try acquiring a ACQUIRE_CAUSES_WAKEUP Wakelock:

PowerManager pm = (PowerManager)mContext.getSystemService(
                                          Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
                                      PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                                      | PowerManager.ACQUIRE_CAUSES_WAKEUP,
                                      TAG);
wl.acquire(1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文