java - Location Manager.RequestLocationUpdates 使用 pendingIntent 始终广播相同的位置,当 gps 提供商需要很长时间才能获取位置时

标签 java android gps android-pendingintent location-provider

我是 Android 开发的新手,我希望我的设备在后台模式下每 40 秒获取一次 GPS 位置,即使在应用程序被终止后也是如此。为此,在 MyAlarm.class 中,我设置了每 40 秒重复一次警报以使用未决 Intent 调用“RepeatingAlarm.class(扩展 BroadcastReceiver)”。在每 40 秒调用一次的“RepeatingAlarm.class”的 onReceive 方法中,我创建了另一个挂起的 Intent 来调用 MyReceiver.class(它扩展了 BroadcastReceiver)。我已将在“RepeatingAlarm.class”中创建的这个未决 Intent 传递给 requestLocationUpdate 函数,以获取 GPS 位置。

我的问题是,有时我会每 40 秒重复获得相同的纬度和经度值,持续至少 3 分钟。

然后,我的 MyReceiver.class 的 onReceive 方法每秒调用一次,而不是在接收到 GPS 位置后调用。我已经在下面粘贴了我的代码,请帮助我解决问题。

MyAlarm.class

public void StartAlarm(Context context)
{
 AlarmManager alm=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
 Intent intent = new Intent(context, RepeatingAlarm.class);
 PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
 alm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 40000, pi);
}

RepeatingAlarm.class

public class RepeatingAlarm extends BroadcastReceiver
{
 public static LocationManager locationManager = null;
 public static PendingIntent pendingIntent = null;
 @Override
 public void onReceive(Context context, Intent intent) 
 {
   locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
   Intent intentp = new Intent(context, MyReceiver.class);
   pendingIntent = PendingIntent.getBroadcast(context, 0, intentp, PendingIntent.FLAG_UPDATE_CURRENT); 
   Criteria criteria = new Criteria(); 
   criteria.setAccuracy(Criteria.ACCURACY_FINE);
   provider = locationManager.getBestProvider(criteria, true);
   if(provider != null)
   {            
    locationManager.requestSingleUpdate(locationManager.GPS_PROVIDER, pendingIntent);
   }
 }
}

MyReceiver.class

public class MyReceiver extends BroadcastReceiver
{
 @Override
 public void onReceive(Context context, Intent intent) 
 {
  String locationKey = LocationManager.KEY_LOCATION_CHANGED;
  if (intent.hasExtra(locationKey)) 
  {

   Location location = (Location)intent.getExtras().get(locationKey);
   double mlatitude = location.getLatitude();
   double mlongitude = location.getLongitude();
   if(RepeatingAlarm.locationManager != null  && RepeatingAlarm.pendingIntent)
   {
     RepeatingAlarm.locationManager.removeUpdates(RepeatingAlarm.pendingIntent);
   }

  }
 }
}

在上面的代码中,GPS 位置每 40 秒接收一次良好。但是,有时,如果 GPS 需要很长时间才能获得位置,比如 15 分钟,之后每 40 秒重复一次相同的先前位置,直到大约 4 分钟。这是我的主要问题。

然后,MyReceiver.class 每秒钟频繁调用。请帮助我提供一些示例代码行来解决此问题。谢谢大家。

最佳答案

根据开发者文档 requestSingleUpdate()方法“使用命名提供者和未决 Intent 注册单个位置更新。”

您需要使用 requestLocationUpdates()方法代替。

此方法的第二个参数位置更新之间的最小时间间隔,以毫秒为单位将允许您一次又一次地获取位置。

关于java - Location Manager.RequestLocationUpdates 使用 pendingIntent 始终广播相同的位置,当 gps 提供商需要很长时间才能获取位置时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22987062/

相关文章:

android - 华为 EMUI 4.0+ 上的电池优化(唤醒锁)

java - 3路归并排序

jar - 我无法在 kubuntu 终端中运行 jar 文件

java - Maven 找不到编译类型的依赖项

Android:计算两个位置之间距离的最佳方法

c# - 从 GPS 读取 NMEA 语句

java - 由于缺少 Spring boot 类,WebLogic 12.2.1.2.0 上的部署失败

android - 无法创建平台 OpenGL 上下文

android - 一个简单的 50 项列表是否足以证明使用 ListView 是合理的?

Android 设备之间的距离(非常准确)