title: How to unregister Listener & stop service from within broadcastreceiver
body: <p>In my App I have a broadcast receiver that upon receiving a keyword in an SMS message, it starts a service that tracks the GPS location of the phone. I do this using -</p>
  
  <pre><code>context.startService(new Intent(context,TrackGPS.class));
  </code></pre>
  
  <p>I also need to be able to stop the service upon receiving another keyword in an SMS message, I have tried to do this but the GPS sensor still tracks the location and GPS icon flashes at the top of the screen. I tried to do this using -  </p>
  
  <pre><code>context.stopService(new Intent(context, TrackGPS.class));
  </code></pre>
  
  <p>I figure this might be because the GPS listener needs to be unregistered. Can anyone help me in getting this service to stop + stop the GPS tracking upon  receipt of a text? Thanks in advance.  </p>
  
  <p><strong>Solution</strong>  </p>
  
  <pre><code>@Override
  public int onStartCommand(Intent GPSService, int flags, int startId) {
      boolean startListening = GPSService.getExtras().getBoolean("IsStartTracking");
      if (startListening == true){
          lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
          lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
      }
      else {
          lm.removeUpdates(this); 
          stopSelf();
      }
      return 1;
  }
  </code></pre>