屏幕关闭时检测手机移动

编程入门 行业动态 更新时间:2024-10-27 14:21:25
本文介绍了屏幕关闭时检测手机移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个应用程序,它应该检测用户的运动,当他停止超过 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.

更多推荐

屏幕关闭时检测手机移动

本文发布于:2023-11-27 01:18:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635975.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:屏幕   手机

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!