屏幕关闭时是否可以检测到运动? [英] Is it possible to detect motion when screen is off?

查看:36
本文介绍了屏幕关闭时是否可以检测到运动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用只需要在屏幕关闭时检测设备的任何运动.

My app simply need to detect any motion of the device while screen is off.

我知道加速度计用于此任务,但在所有设备的屏幕关闭时它都不起作用.这是一个设备列表 http://www.saltwebsites.com/2012/android-加速度计屏幕关闭

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.

你可以这样使用它:

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

如果您在 Service,您可以简单地在 onCreate() 中获取锁定并在 onDestroy() 中释放.

If you obtain accelerometer data in a Service, you could simply acquire lock in it's onCreate() and release in onDestroy().

这篇关于屏幕关闭时是否可以检测到运动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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