In my application, I have a broadcast receiver that, after receiving a keyword in an SMS message, starts a service that tracks the GPS location of the phone. I use this -
context.startService(new Intent(context,TrackGPS.class));
I also need to be able to stop the service after receiving another keyword in the SMS message, I tried to do this, but the GPS sensor is still tracking the location and the GPS icon is blinking at the top of the screen. I tried to do this with
context.stopService(new Intent(context, TrackGPS.class));
I suppose this could be because the GPS listener must be unregistered. Can someone help me in getting this service to stop + stop GPS tracking after receiving the text? Thank you in advance.
Decision
@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; }
source share