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

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

问题描述

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

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

所以我需要一个解决方案来检测用户移动,即使是大多数设备上的屏幕关闭。有关如何实现这一点的任何想法?



感谢转发

解决方案

您应该获得部分唤醒锁进行这种操作。

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

您还需要 AndroidManifest.xml

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

建议使用 lock.release();
$ b

编辑:



另外, 文章可能对您有用。


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

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?

Thanks in forward

解决方案

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

Something like this:

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

You need also this permission in the AndroidManifest.xml:

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

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

EDIT:

Also, this article could be useful for you.

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

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