title: Android - Update Location every x minutes
body: <p>I'm pretty new to android, and this is my first post here, so please be kind! :-)</p>
  
  <p>I'm trying to create a service which runs in the background and does a location update every x minutes. To run it every x minutes I'm using the AlarmManager, as described here: <a href="https://stackoverflow.com/questions/4459058/alarm-manager-example">Alarm Manager Example</a></p>
  
  <p>Here's what I've got: </p>
  
  <pre><code>package com.example.service1;
  
  import android.app.AlarmManager;
  import android.app.PendingIntent;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.os.PowerManager;
  import android.util.Log;
  
  public class Scheduler extends BroadcastReceiver{
  
  LocationManager locationManager;
  LocationListener locationListener;
  
  
  @Override
  public void onReceive(Context context, Intent intent) {
      // TODO Auto-generated method stub
      PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
      PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
      wl.acquire();
  
  
  
  
      //Code which is executed every X seconds/minutes
  
      getLocation(context);
  
      //End of Code
      wl.release();
  
  }
  
  public void setScheduler(Context context) {
      AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      Intent i = new Intent(context, Scheduler.class);
      PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 20, pi);
  
  }
  
  
  //Method to get the Location
  
  public void getLocation(Context context) {
  
      Log.e("null","getLocation");
      locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  
      locationListener = new LocationListener() {
  
          @Override
          public void onLocationChanged(Location location) {
              // TODO Auto-generated method stub
              Log.e(null, "location change");
              makeUseOfLocation(location);
          }
  
          @Override
          public void onProviderDisabled(String provider) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void onProviderEnabled(String provider) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void onStatusChanged(String provider, int status,
                  Bundle extras) {
              // TODO Auto-generated method stub
  
          }
  
      };
  
  }
  
  //Method to work with the location Data; Instance of Point is created
  public void makeUseOfLocation(Location location) {
      Log.e(null,"makeuse");
  
      Log.e(null,location.getLatitude() + "");
  }
  
  }
  </code></pre>
  
  <p><code>getLocation()</code> is called every 20 seconds, but then it never runs <code>onLocationChanged()</code> (I use the EmulatorControl in Eclipse to change the location).</p>
  
  <p>I had the same problem before when I used the <code>ScheduledExecutorService</code> instead of the <code>AlarmManager</code>. </p>
  
  <p>Can anyone help me?</p>