打开/关闭屏幕在Android中使用摇一摇 [英] turn the screen on/off in Android with a shake

查看:300
本文介绍了打开/关闭屏幕在Android中使用摇一摇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在需要时,用户摇动手机打开/关闭屏幕的应用程序。到目前为止,我已经得到了作为答案建议侦听甩头一个SensorEventListener这个问题

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.

当我发现一摇,我改变屏幕的亮度值作为<建议href=\"http://stackoverflow.com/questions/1875669/calling-hidden-api-in-android-to-turn-screen-off\">this问题的答案。如果我真的不关闭屏幕...如果我通过它的工作原理公共无效setBright(浮点值)方法亮度设置为0.01F这一切的伟大工程完美。但是,如果我在亮度设置为0.0,手机会不会再次打开屏幕...直到我preSS电源按钮,至少。

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?

- 编辑 -

感谢德瑞并的 icyerasor 我再进去看了问题。我先拿 PARTIAL_WAKE_LOCK 之前,我的亮度设置为0.0女,但它仍然无法开机时,我摇动手机上。然而,当我调试应用程序,我看到setBright(1.0F)被称为还好吧,当我摇动手机屏幕关闭;我怀疑是锁定画面以某种方式与它搞乱,因为它在我preSS电源按钮踢。之后我preSS电源键,应用程序继续工作,因为它通常不会。有没有办法绕过锁屏?

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?

感谢您的帮助!

推荐答案

只是一个猜测:设置它brighnes 0.0也可以将手机睡眠模式

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

当你想打开它重新编程,尝试获取ACQUIRE_CAUSES_WAKEUP激活锁定:

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);

这篇关于打开/关闭屏幕在Android中使用摇一摇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆