title: Location Manager.RequestLocationUpdates using pendingIntent always broadcasting the same location, when gps provider takes long time to get location
body: <p>I am new to android development, I want my device to get GPS location every 40 seconds in background mode, even after the app got killed. For that, In MyAlarm.class, I have setRepeating the Alarm every 40 seconds to call a "RepeatingAlarm.class(which extends BroadcastReceiver)" using the pending intent. In the onReceive method of the "RepeatingAlarm.class" called every 40 seconds, I have created another pending intent to call MyReceiver.class(which extends BroadcastReceiver). I have passed this pending intent created in the "RepeatingAlarm.class" to a requestLocationUpdate function, to get the GPS location.</p>
  
  <p>My problem is that, sometimes I will get the same lat and long values repeatedly every 40 seconds continuing about atleast 3 minutes.</p>
  
  <p>Then, the onReceive method of my MyReceiver.class is calling every second, instead of calling once the GPS location got received. I have pasted my code below, Please help me with a solution.</p>
  
  <pre><code>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  &amp;&amp; RepeatingAlarm.pendingIntent)
     {
       RepeatingAlarm.locationManager.removeUpdates(RepeatingAlarm.pendingIntent);
     }
  
    }
   }
  }
  </code></pre>
  
  <p>In the above code, the GPS location is receiving good once in every 40 seconds. But, sometimes, if once GPS takes long time to get the location say 15 minutes, after that every 40 seconds the same  previous location is repeating till about 4 minutes. This is my major problem.</p>
  
  <p>Then, the MyReceiver.class is calling frequently every seconds. Please help me with some example lines of code to solve this issue. Thank You all.</p>