GPS Location tracking even when app is closed(Not running in Background)/ScreenLocked(GPS定位跟踪,即使应用程序关闭(不在后台运行)/屏幕锁定)
问题描述
我想跟踪与 strava 非常相似的用户位置,即使在关闭之后也是如此.
I want to track user location very similar to strava, even after the is closed.
我尝试了 AlarmManager,但它没有每分钟执行一次
I tried AlarmManager but it's not giving me execution after every one minute
推荐答案
如果设备进入睡眠模式,仅使用 BiGGZ 解释的服务将不起作用.虽然服务不会被杀死,但您的应用程序不会获得任何 CPU.因此,您必须获取部分唤醒锁以防止设备进入睡眠模式.
Just using a Service as BiGGZ explained won't work in case the device enters sleep mode. The Service won't be killed though but your app won't get any CPU. Therefore you would have to acquire a partial Wakelock to prevent the device from entering sleep mode.
@Override
public void onCreate() {
super.onCreate();
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = m.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Some Tag");
wakeLock.acquire();
...
}
@Override
public void onDestroy() {
wakeLock.release();
}
如果您需要的位置更新频率较低,您可以使用 AlarmManager 并设置触发位置更新的重复警报.警报应该触发一个 BroadcastReceiver,因为它不能保证在设备再次进入睡眠状态之前成功启动服务.见这里:https://developer.android.com/reference/android/app/AlarmManager.html
If you need Location updates less frequently you could use the AlarmManager and set a repeating alarm which triggers a Location update. The alarm should trigger then a BroadcastReceiver, since it's not guaranteed that a Service is successfully started before the device goes back to sleep again. See here: https://developer.android.com/reference/android/app/AlarmManager.html
这篇关于GPS定位跟踪,即使应用程序关闭(不在后台运行)/屏幕锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:GPS定位跟踪,即使应用程序关闭(不在后台运行)/屏幕锁定
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
