屏幕关闭时检测手机移动 [英] Detect phone movement when screen is off

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

问题描述

我正在开发一个应用程序,它应该检测用户的运动,当他停止超过 5 分钟时,它应该发出警报.我能够用加速度计检测到运动,但问题是当屏幕关闭时它不起作用.我也尝试过使用部分唤醒锁.重新注册加速度计也不起作用(这应该是摩托罗拉设备的解决方法).

I am developing an application which should detect user movement and when he stops for more than 5 minutes it should sound an alarm. I was able to detect movement with accelerometer but the problem is it doesnt work when the screen is off. I have also tried using partial wakeLock. Re-registering accelerometer doesnt work either (this should be workaround for motorola devices).

也许我可以使用 GPS 做到这一点,并在 GPS 速度低于 1m/s 超过 5 分钟时发出警报,但我不确定在屏幕关闭时我是否会收到 GPS 更新.

Maybe I can do this using GPS and sound an alarm when GPS speed is less than 1m/s for more than 5 minutes but I am not sure if I will receive GPS updates when screen is off.

所以我需要一个解决方案,即使在大多数设备上的屏幕关闭时也能检测到用户的移动.关于如何实现这一点的任何想法?

So I need a solution that will detect user movement even is screen is off on most devices. Any ideas on how to acomplish this?

谢谢转发

推荐答案

您应该为此类操作获取部分唤醒锁.使用 PowerManager 类.

You should acquire a partial wake lock for this kind of operation. Use the PowerManager class.

像这样:

PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,  "SensorRead");
lock.acquire();

您还需要在 AndroidManifest.xml 中获得此权限:

You need also this permission in the AndroidManifest.xml:

 <uses-permission android:name="android.permission.WAKE_LOCK" />

建议在完成工作后使用 lock.release();.

Is recommendable using lock.release(); when you're done your work.

此外,这篇文章可能对您有用.

Also, this article could be useful for you.

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

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