在 Android 中通过摇动打开/关闭屏幕
我正在制作一个应用程序,需要在用户摇动手机时打开/关闭屏幕。到目前为止,我有一个 SensorEventListener 可以按照 的答案中的建议监听震动这个问题。
当我检测到震动时,我会按照
我想做的事情可能吗?我做错了什么?
-- 编辑 --
感谢 Dre 和 icyerasor 我已经进一步研究了这个问题。在将亮度设置为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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不过,我同意 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.0I would test this before answering but I don't have access to an Android device at this moment.
只是猜测:将其设置为 0.0 也可能使手机进入睡眠模式?
当您想以编程方式再次打开它时,请尝试获取 ACQUIRE_CAUSES_WAKEUP 唤醒锁:
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: