title: How to continuously track the location of an Android mobile phone?
body: <p>I need to write an app, which every 5 minutes determines the current location of the mobile phone (using all free, available location providers) and sends it to a server.</p>
  
  <p>If some location provider doesn't work at the start of the application, but becomes available later, the application should process its location data as well.</p>
  
  <p>In order to do this, I implemented following class. In one of my activities, I create an instance of it and call its <code>startTrackingUpdates</code> method. <code>locationChangeHandler</code> does the processing of location data.</p>
  
  <pre><code>public class LocationTracker implements ILocationTracker, LocationListener {
      public static final long MIN_TIME_BETWEEN_UPDATES = 1000 * 60 * 5; // 5 minutes
      public static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
      private ILocationManager locationManager;
      private ILocationChangeHandler locationChangeHandler;
  
      public LocationTracker(final ILocationManager aLocationManager,
              final ILocationChangeHandler aLocationChangeHandler) {
          this.locationManager = aLocationManager;
          this.locationChangeHandler = aLocationChangeHandler;
      }
  
      public void startTrackingUpdates() {
          final List&lt;String&gt; providers = locationManager.getAllProviders();
  
          for (final String curProviderName : providers)
          {
              final ILocationProvider provider = locationManager.getLocationProvider(curProviderName);
  
              if (!provider.hasMonetaryCost())
              {
                  subscribeToLocationChanges(provider);
              }
          }
      }
  
      private void subscribeToLocationChanges(final ILocationProvider aProvider) {
          locationManager.requestLocationUpdates(aProvider.getName(), MIN_TIME_BETWEEN_UPDATES, 
                  (float)MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
      }
  
      public void onLocationChanged(final Location aLocation) {
          locationChangeHandler.processLocationChange(new LocationWrapper(aLocation));
      }
  
      public void onProviderDisabled(final String aProvider) {
      }
  
      public void onProviderEnabled(final String aProvider) {
      }
  
      public void onStatusChanged(final String aProvider, final int aStatus, 
              final Bundle aExtras) {
      }
  }
  </code></pre>
  
  <p>I built the application and installed it on my mobile phone. Then, in the morning I took my phone, went to work, then went back home. At home I checked the results of a day-long work of the application and found only one record in the database.</p>
  
  <p>Provided that the rest of the system (transmission of location data to the server, saving in the database) works correctly, I must have some problem in the location tracking code (<code>onLocationChanged</code> wasn't invoked even though I changed my location several times).</p>
  
  <p>What's wrong with my code (how should I change it, in order for the <code>onLocationChanged</code> to be called whenever the location of the phone changes by more than <code>MIN_DISTANCE_CHANGE_FOR_UPDATES</code> meters according to one of the locatin providers) ?</p>
  
  <p><strong>Update 1 (18.08.2013 13:59 MSK):</strong></p>
  
  <p>I changed my code according to <a href="https://stackoverflow.com/users/943690/msh">msh</a> recommendations. I start the timer using following code:</p>
  
  <pre><code>public void startSavingGeoData() {
      final IAlarmManager alarmManager = activity.getAlarmManager();
      final IPendingIntent pendingIntent = activity.createPendingResult(
              ALARM_ID, intentFactory.createEmptyIntent(), 0);
      alarmManager.setRepeating(INTERVAL, pendingIntent);
  }
  </code></pre>
  
  <p>In the <code>activity</code> I put following timer event handler:</p>
  
  <pre><code>@Override
  protected void onActivityResult(final int aRequestCode, final int aResultCode, 
          final Intent aData) {
      geoDataSender.processOnActivityResult(aRequestCode, aResultCode, 
              new IntentWrapper(aData));
  }
  </code></pre>
  
  <p>When the phone is turned on, everything works as expected - <code>onActivityResult</code> is executed once in <code>INTERVAL</code> milliseconds.</p>
  
  <p>But when I press the power button (screen becomes disabled), <code>onActivityResult</code> is not invoked at all. When I press the power button again, <code>onActivityResult</code> is executed as many times, as <code>INTERVAL</code> has passed since the time when I turned the phone off. If I turned the phone off for <code>1 * INTERVAL</code> milliseconds, it will fire once. If I turned the phone off for <code>2 * INTERVAL</code> milliseconds, it will fire twice etc.</p>
  
  <p>Note: By "turning off" I mean a short press of the power button (the phone still "lives" and reacts to incoming SMS, for example).</p>
  
  <p>How should I modify the code of <code>startSavingGeoData</code> in order for the method <code>onActivityResult</code> to be executed every <code>INTERVAL</code> milliseconds, even when the phone sleeps?</p>
  
  <p><strong>Update 2 (18.08.2013 19:29 MSK):</strong> Neither <code>alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, INTERVAL, pendingIntent)</code>, nor <code>alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, INTERVAL, INTERVAL, pendingIntent)</code> solved the problem.</p>