即使应用程序关闭

即使应用程序关闭

本文介绍了GPS位置跟踪,即使应用程序关闭(不在后台运行)/ ScreenLocked的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪与strava非常相似的用户位置,即使在关闭后也是如此。



我尝试了AlarmManager,但每隔一分钟后都不会执行。

解决方案
只要使用服务作为BiGGZ解释将不会工作,以防设备进入睡眠模式。该服务不会被杀死,但你的应用程序不会得到任何CPU。因此,您必须购买部分Wakelock以防止设备进入睡眠模式。

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



$ b $ p
$ b

如果您需要频繁使用位置更新,可以使用AlarmManager并设置重复触发位置更新的警报。该警报应该触发BroadcastReceiver,因为无法保证服务在设备再次返回到睡眠状态之前已成功启动。看到这里:


I want to track user location very similar to strava, even after the is closed.

I tried AlarmManager but it's not giving me execution after every one minute

解决方案

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

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位置跟踪,即使应用程序关闭(不在后台运行)/ ScreenLocked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:39