title: Android Periodic GPS location updates with AlarmManager inside a Service
body: <p>I read a lot of questions on here but couldn't figure out what the problem is.</p>
  
  <p>I'm writing a field service application for Android. In one of the Activities (MyActivity.java) I have two buttons: Start and Stop. </p>
  
  <p>When the field worker presses start I need to get his current location with GPS and send it to server in timely intervals (say it be 5 minutes default, here I set it to 20 secs. for testing). The customer wants this to see how long the workers spend time in traffic, the traffic in my city (Istanbul) is a mess.</p>
  
  <p>I have an AlarmManager in my activity, when the start button is pressed the alarm is set by <code>AlarmManager.setRepeating()</code> to start the service (ServiceClass.java).</p>
  
  <pre><code>Intent intent = new Intent (MyActivity.this, ServiceClass.class);
  mPendingIntent = PendingIntent.getService(MyActivity.this, 0, intent,
                                                PendingIntent.FLAG_CANCEL_CURRENT);
  mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  mAlarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 20*1000,
                                                                    mPendingIntent);
  </code></pre>
  
  <p>I cancel the Alarm with the Stop button. </p>
  
  <p>It works perfectly till here, the Service starts. Its <code>onCreate()</code>, <code>onStart()</code> and <code>onDestroy()</code> methods are executed. But I can't get the location. I'm not working on a real device so I'm using DDMS to send locations. But the application skips that step and prints Longitude and Latitude of my location as Lon:0 Lat:0. </p>
  
  <p>Here's the code inside the service:</p>
  
  <pre><code>public void onStart(Intent intent, int startId) {
      super.onStart(intent, startId);
      Log.d("Testing", "Service got started, calling location updates:");
      mLocationManager.requestSingleUpdate(mCriteria, mLocationListener,
                                                                  getMainLooper());
      Log.i("TESTING LOCATION UPDATE: LOCATION:", "\nLat:"   + mLatitude +
                                                            "  Lon:" + mLongitude);
      stopSelf(startId);
      Log.d("Testing", "Service Stopped!");
  </code></pre>
  
  <p>And finally, here's my LocationListener:</p>
  
  <pre><code> mLocationListener = new LocationListener() {
          @Override
          public void onLocationChanged(Location location) {
  
              if(location != null){
                  mLatitude = location.getLatitude();
                  mLongitude = location.getLongitude();
                  Log.i("onLocationChanged(Location location)", "Lat:"+mLatitude + "
                                                              Lon:" + mLongitude);
  }
  </code></pre>
  
  <p>Also something caught my eye, when I run the code:</p>
  
  <pre><code>mLocationProvider = mLocationManager.getBestProvider(mCriteria, true);
  Log.i("BEST PROVIDER IS:", mLocationProvider);
  </code></pre>
  
  <p>It says BEST PROVIDER IS: gps. But this log inside onProviderEnabled() is never shown in logcat.</p>
  
  <pre><code>@Override
  public void onProviderEnabled(String s) {
      Log.v("onProviderEnabled", "ENABLED");
  }
  </code></pre>
  
  <p>Two things to add, if I use:</p>
  
  <pre><code>mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                                                                mLocationListener);
  </code></pre>
  
  <p>it doesn't work either.</p>
  
  <p>This however works and I can get the LastKnownLocation:</p>
  
  <pre><code>Location lastKnown = mLocationManager.getLastKnownLocation(
                                                     LocationManager.GPS_PROVIDER);
  </code></pre>
  
  <p>First of all, is this a good approach for doing this? If it is please check to see what I am missing. If it is not, can you please tell me how can I implement this any better?</p>
  
  <p>Thank you. :)</p>