Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当gps提供商需要很长时间才能获取位置时,Location Manager.RequestLocation使用PendingEvent更新始终广播相同的位置_Java_Android_Gps_Android Pendingintent_Location Provider - Fatal编程技术网

Java 当gps提供商需要很长时间才能获取位置时,Location Manager.RequestLocation使用PendingEvent更新始终广播相同的位置

Java 当gps提供商需要很长时间才能获取位置时,Location Manager.RequestLocation使用PendingEvent更新始终广播相同的位置,java,android,gps,android-pendingintent,location-provider,Java,Android,Gps,Android Pendingintent,Location Provider,我是android开发的新手,我希望我的设备在后台模式下每40秒获得一次GPS定位,即使在应用程序死机后也是如此。为此,在MyAlarm.class中,我设置了每隔40秒重复一次报警,以使用挂起的意图调用“RepeatingAlarm.class(它扩展了BroadcastReceiver)”。在每隔40秒调用一次的“RepeatingAlarm.class”的onReceive方法中,我创建了另一个调用MyReceiver.class(扩展了BroadcastReceiver)的挂起意图。我已

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

我的问题是,有时我会每隔40秒重复获得相同的lat和long值,至少持续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每秒钟频繁调用一次。请帮助我用一些示例代码行来解决这个问题。谢谢大家。

按照开发人员文档方法“使用指定的提供程序和挂起的意图注册单个位置更新”

您需要改用方法


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

我也尝试了requestLocationUpdates()函数。但是,重复的位置仍然存在。你能告诉我,我的密码里有播放悬挂式帐篷的方法吗?在StartAlarm.class和RepeatingAlarm.class中。谢谢。@Raj:如果要使用
requestLocationUpdates()
,那么就不需要AlarmManager方法。这是一个或另一个,不是两个都有。这就是为什么会得到重复的位置。我想使用requestSingleUpdates()函数,AlarmManager每隔40秒调用一次。然后,MyReceiver.class中的OnReceive方法从请求SingleUpdates()被调用到获取gps的那一刻,每秒钟都在调用一次。@Chungpham:你能告诉我为什么在gps花费很长时间监听之后,位置会从一点复制(即给出相同的位置)吗(在公共汽车上、火车上或更拥挤的公共场所)。我认为我的代码中有错误,但我无法预测。谢谢。也许这解释了
requestLocationUpdates()的原因
方法有效-尤其要注意第一个参数
minTime
。简而言之,
minTime
参数已经起到了“计时器”的作用,并且由于您还使用AlarmManager来安排位置更新,因此,您将同时获得两个位置更新。如果您想知道GPS何时获得第一个定位,您可以使用我对这个问题的回答中的代码:@cYrixmorten:您能告诉我为什么位置重复(即,提供相同的位置)从GPS需要很长时间才能收听(在公共汽车上、火车上或更拥挤的公共场所)。我认为我的代码中有错误,但我无法预测。谢谢。老实说,我现在还说不出原因。我有一段时间没有涉入其中,但我怀疑(尽管有标准。准确度\精细标准),只要GPS没有找到第一个定位,它就会返回lastKnownLocation。这些位置上的不可靠结果是我费尽周折编写一些代码的原因,这些代码会准确地告诉我设备何时能够提供准确的位置。很抱歉,没有正确阅读问题,关于重复的问题,这听起来像是错误的很可能是Chungpham在Kedarnath的回复中写道的。顺便说一句,如果你的设备在寻找GPS时通常很慢,并且它运行的是CyangenMod或其他根目录,那么它可能使用了错误的AGPS区域服务器。要解决这个问题,你可以从Google Play下载FasterFix。我运行的是CyangenMod,在欧洲生活,它是在北美使用这台服务器使得它在寻找GPS信号时速度非常慢。