是否有可能检测到运动时屏幕关闭? [英] Is it possible to detect motion when screen is off?
问题描述
我的应用程序只需要检测到设备的任何运动,而屏幕是关闭的。
My app simply need to detect any motion of the device while screen is off.
我知道,加速度计用于此任务,但同时屏幕关闭所有设备中它不工作。这是设备的列表 http://www.saltwebsites.com/2012/android -accelerometers屏幕关闭
I know that accelerometer is used for this task but it don't work while screen is off in all devices. this is a list of devices http://www.saltwebsites.com/2012/android-accelerometers-screen-off
所以有服用加速度传感器数据的方式,而屏幕是关闭了所有设备的工作原理?
so is there a way of taking accelerometer sensor data while screen is off that works on all devices?
还是有办法使用另一个传感器来检测运动?
or is there a way to detect motion using another sensors?
推荐答案
部分唤醒锁定
是所有你需要访问加速度计读数而屏幕是关闭的。
Partial Wake Lock
is all you need to access accelerometer readings while the screen is off.
您可以使用它是这样的:
You can use it like this:
private PowerManager.WakeLock mWakeLock;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
mWakeLock.acquire();
和大功告成后,就释放锁:
And after you're done, just release the lock:
mWakeLock.release();
如果您在 获得加速度数据服务
,你可以简单地获得锁在它的的onCreate()
和发布的onDestroy()
这篇关于是否有可能检测到运动时屏幕关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!