title: Location listener works from a Service but not an IntentService
body: <p>I have an app where I am trying to periodically get user location and send to server. I have a service attached to an <code>AlarmManager</code> which executes every minute (for testing). The service finds the user location correctly and logs out the GPS co-ords. Once there is a GPS lock I cancel the location request and stop the service. When I ask for location updates, I start a <code>Handler</code> that executes 20 seconds later, this <code>Handler</code> removes the location update and stops the <code>Service</code> in case no lock is achieved. All this works.</p>
  
  <p>Below is the code that works using the <code>Service</code> class.</p>
  
  <pre><code>public class TrackingService extends Service {
  
      private static final String TAG = TrackingService.class.getSimpleName();
      LocationManager             mlocManager;
      LocationListener            mlocListener;
      NfcScannerApplication       nfcscannerapplication;
      String carerID;
  
      Handler endServiceHandler;
      Runnable endServiceRunnable;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          nfcscannerapplication = (NfcScannerApplication) getApplication();
          mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          mlocListener = new MyLocationListener();
  
          Log.e(TAG, "Service created and location manager and listener created");
  
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.e(TAG, "in onDestroy in LocationService class");
          mlocManager.removeUpdates(mlocListener);
  
  
      }
  
      @Override
      public void onStart(Intent intent, int startId) {
          super.onStart(intent, startId);
          mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
          Log.e(TAG, "requesting location updates");
  
          enableMenuButtonsHandler();
          endServiceHandler.postDelayed(endServiceRunnable,20 * 1000);
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      private class MyLocationListener implements LocationListener {
  
          @Override
          public void onLocationChanged(Location loc) {
  
  
              Log.e(TAG, "in TrackingService onlocationChanged and about to send lon/lat " + loc.getLongitude() + " " + loc.getLatitude());
  
  
              DateTime dt = new DateTime();
              DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
              String formattedNowTime3 = df3.print(dt);
              Log.e(TAG, "Time of location fix in TrackingServive = " + formattedNowTime3);
  
  
              Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
  
              if (c.getCount() &gt; 0) {
                  if(c.moveToLast()){
  
                  carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
  
                  }
              }
  
               Log.e(TAG, "carer ID = " + carerID);
  
               mlocManager.removeUpdates(mlocListener);
               Log.e(TAG, "removed updates(TrackingService)");
  
               TrackingService.this.stopSelf();
               Log.e(TAG, "called stopSelf on TrackingService");
  
          }
  
          @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
  
          }
  
      }// end of MyLocationListener
  
  
      public void enableMenuButtonsHandler() {
  
          endServiceHandler = new Handler();
          endServiceRunnable = new Runnable() {
              public void run() {
  
                  endService();
  
              }
  
              private void endService() {
  
                   mlocManager.removeUpdates(mlocListener);
                   Log.e(TAG, "removed updates(TrackingService) from the endService handler");
  
                   TrackingService.this.stopSelf();
                   Log.e(TAG, "called stopSelf on TrackingService from the endService handler");
  
              }
          };
  
      }
  
  
  }// end of service 
  </code></pre>
  
  <p>The problem I am having is once I have a GPS lock, I want to send the long and lat co-ords to the server. I know I can use <code>AsyncTask</code> from a <code>Service</code> but I don't want to do this. I would prefer to use <code>IntentService</code> as it runs in its own background thread. This way I can make network call directly from the <code>IntentService</code>. </p>
  
  <p>The following code implements the <code>IntentService</code> class but it doesn't seem to get a lock. The GPS keeps flashing on the phone indefinitely. The logging statements get as far as 'requesting location updates', then nothing after that.</p>
  
  <p>Has anyone any ideas why no lock is achieved? Thanks in advance.</p>
  
  <pre><code>public class TrackingService extends IntentService {
  
     private static final String TAG = TrackingService.class.getSimpleName();
     LocationManager             mlocManager;
     LocationListener            mlocListener;
     NfcScannerApplication       nfcscannerapplication;
     String carerID;
  
     Handler endServiceHandler;
     Runnable endServiceRunnable;
  
      public TrackingService() {
          super("TrackingService");
  
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {
  
             nfcscannerapplication = (NfcScannerApplication) getApplication();
             mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
             mlocListener = new MyLocationListener();
             Log.e(TAG, "Service created and location manager and listener created");
  
             mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
             Log.e(TAG, "requesting location updates");
  
             enableMenuButtonsHandler();
             endServiceHandler.postDelayed(endServiceRunnable,20 * 1000);
  
      }
  
  
  
       private class MyLocationListener implements LocationListener {
  
                   @Override
                   public void onLocationChanged(Location loc) {
  
  
                      Log.e(TAG, "in TrackingService onlocationChanged and about to send lon/lat " + loc.getLongitude() + " " + loc.getLatitude());
  
  
                       DateTime dt = new DateTime();
                       DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
                       String formattedNowTime3 = df3.print(dt);
                       Log.e(TAG, "Time of location fix in TrackingServive = " + formattedNowTime3);
  
  
                       Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
  
                      if (c.getCount() &gt; 0) {
                          if(c.moveToLast()){
  
                          carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
  
                          }
                      }
  
                       Log.e(TAG, "carer ID = " + carerID);
  
                       mlocManager.removeUpdates(mlocListener);
                       Log.e(TAG, "removed updates(TrackingService)");
  
                       TrackingService.this.stopSelf();
                       Log.e(TAG, "called stopSelf on TrackingService");
  
                   }
  
                   @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
  
                   }
  
               }// end of MyLocationListener
  
  
       public void enableMenuButtonsHandler() {
  
                  endServiceHandler = new Handler();
                  endServiceRunnable = new Runnable() {
                      public void run() {
  
                          endService();
  
                      }
  
                      private void endService() {
  
                           mlocManager.removeUpdates(mlocListener);
                           Log.e(TAG, "removed updates(TrackingService) from the endService handler");
  
                           TrackingService.this.stopSelf();
                           Log.e(TAG, "called stopSelf on TrackingService from the endService handler");
  
                      }
                  };
  
       }
  
  }//end of trackingService
  </code></pre>
  
  <p>[Edit1]</p>
  
  <pre><code>public class TrackingService extends Service {
  
      private static final String TAG = TrackingService.class.getSimpleName();
      LocationManager             mlocManager;
      LocationListener            mlocListener;
      NfcScannerApplication       nfcscannerapplication;
      String carerID;
  
      Handler endServiceHandler;
      Runnable endServiceRunnable;
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
  
          nfcscannerapplication = (NfcScannerApplication) getApplication();
          mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          mlocListener = new MyLocationListener();
  
          Log.e(TAG, "Service created and location manager and listener created");
  
          mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
          Log.e(TAG, "requesting location updates");
  
          enableMenuButtonsHandler();
          endServiceHandler.postDelayed(endServiceRunnable, 20 * 1000);
  
              return super.onStartCommand(intent, flags, startId);
      }
  
  
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.e(TAG, "in onDestroy in LocationService class");
          mlocManager.removeUpdates(mlocListener);
  
  
      }
  
  
  
  
  
  
  
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      private class MyLocationListener implements LocationListener {
  
          @Override
          public void onLocationChanged(Location loc) {
  
  
              Log.e(TAG, "in TrackingService onlocationChanged and about to send lon/lat " + loc.getLongitude() + " " + loc.getLatitude());
  
  
              DateTime dt = new DateTime();
              DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
              String formattedNowTime3 = df3.print(dt);
              Log.e(TAG, "Time of location fix in TrackingServive = " + formattedNowTime3);
  
  
              Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
  
              if (c.getCount() &gt; 0) {
                  if(c.moveToLast()){
  
                  carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
  
                  }
              }
  
               Log.e(TAG, "carer ID = " + carerID);
  
               mlocManager.removeUpdates(mlocListener);
               Log.e(TAG, "removed updates(TrackingService)");
  
               TrackingService.this.stopSelf();
               Log.e(TAG, "called stopSelf on TrackingService");
  
          }
  
          @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
  
          }
  
      }// end of MyLocationListener
  
  
      public void enableMenuButtonsHandler() {
  
          endServiceHandler = new Handler();
          endServiceRunnable = new Runnable() {
              public void run() {
  
                  endService();
  
              }
  
              private void endService() {
  
                   mlocManager.removeUpdates(mlocListener);
                   Log.e(TAG, "removed updates(TrackingService) from the endService handler");
  
                   TrackingService.this.stopSelf();
                   Log.e(TAG, "called stopSelf on TrackingService from the endService handler");
  
              }
          };
  
      }
  
  
  }// end of service 
  </code></pre>
  
  <p>[Edit2]</p>
  
  <pre><code>@Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
  
          nfcscannerapplication = (NfcScannerApplication) getApplication();
          mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          mlocListener = new MyLocationListener();
          Log.e(TAG, "Service created and location manager and listener created");
  
  
              HandlerThread handlerThread = new HandlerThread("MyHandlerThread");
              handlerThread.start();
              Looper looper = handlerThread.getLooper();
  
              Handler handler = new Handler(looper);
  
          mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener, looper);
          Log.e(TAG, "requesting location updates");
  
          enableMenuButtonsHandler();
          endServiceHandler.postDelayed(endServiceRunnable, 20 * 1000);
  
              return super.onStartCommand(intent, flags, startId);
      }
  </code></pre>
  
  <p>[Edit3]</p>
  
  <pre><code>public class TrackingService extends Service {
  
      private static final String TAG = TrackingService.class.getSimpleName();
      LocationManager             mlocManager;
      LocationListener            mlocListener;
      NfcScannerApplication       nfcscannerapplication;
      String carerID;
  
      Handler endServiceHandler;
      Runnable endServiceRunnable;
      HandlerThread handlerThread;
      Looper looper;
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
  
          nfcscannerapplication = (NfcScannerApplication) getApplication();
          mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          mlocListener = new MyLocationListener();
          Log.e(TAG, "Service created and location manager and listener created");
  
  
              Log.e(TAG, "creating handlerthread and looper");
              handlerThread = new HandlerThread("MyHandlerThread");
              handlerThread.start();
              looper = handlerThread.getLooper();
  
  
  
  
  
          mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener, looper);
          Log.e(TAG, "requesting location updates");
  
          enableMenuButtonsHandler();
          endServiceHandler.postDelayed(endServiceRunnable, 20 * 1000);
  
              return super.onStartCommand(intent, flags, startId);
      }
  
  
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.e(TAG, "in onDestroy in LocationService class");
          mlocManager.removeUpdates(mlocListener);
  
  
      }
  
  
  
  
  
  
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      private class MyLocationListener implements LocationListener {
  
          @Override
          public void onLocationChanged(Location loc) {
  
  
              Log.e(TAG, "in TrackingService onlocationChanged and about to send lon/lat " + loc.getLongitude() + " " + loc.getLatitude());
  
  
              DateTime dt = new DateTime();
              DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
              String formattedNowTime3 = df3.print(dt);
              Log.e(TAG, "Time of location fix in TrackingServive = " + formattedNowTime3);
  
  
              Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
  
              if (c.getCount() &gt; 0) {
                  if(c.moveToLast()){
  
                  carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
  
                  }
              }
  
               Log.e(TAG, "carer ID = " + carerID);
  
               nfcscannerapplication.loginWebservice.sendCarerLocation(carerID, formattedNowTime3, String.valueOf(loc.getLatitude()), String.valueOf(loc.getLongitude()));
  
  
  
               Log.e(TAG, "quiting handlerthread");
               handlerThread.quit();
  
               mlocManager.removeUpdates(mlocListener);
               Log.e(TAG, "removed updates(TrackingService)");
  
               TrackingService.this.stopSelf();
               Log.e(TAG, "called stopSelf on TrackingService");
  
          }
  
          @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
  
          }
  
      }// end of MyLocationListener
  
  
      public void enableMenuButtonsHandler() {
  
          endServiceHandler = new Handler();
          endServiceRunnable = new Runnable() {
              public void run() {
  
                  endService();
  
              }
  
              private void endService() {
  
                   mlocManager.removeUpdates(mlocListener);
                   Log.e(TAG, "removed updates(TrackingService) from the endService handler");
  
                   TrackingService.this.stopSelf();
                   Log.e(TAG, "called stopSelf on TrackingService from the endService handler");
  
                   Log.e(TAG, "quiting handlerthread from the endService handler");
                   handlerThread.quit();
  
              }
          };
  
      }
  
  
  }// end of service
  </code></pre>