Skip to content Skip to sidebar Skip to footer

How To Unregister Listener & Stop Service From Within Broadcastreceiver

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 - context.star

Solution 1:

I don't think your issue is stopping the service. By killing your service you are not killing the LocationListener object that is still looking for updates.

I would do the following.

Instead of stopping and starting your services, simply start your service every time you need to do something (Start listening or stop listening).

Add an extra to the Intent you use to tell you what you want to do, something like:

Intent GPSService = newIntent(context, TrackGPS.class);
GPSService.putExtra("IsStartTracking", true);
context.startService(GPSService);   

In your onStart method override of your service, you can now register and unregister location listeners on a single instance of your LocationManager based on the value of "IsStartTracking"

booleanstartListening= intent.getExtras().getBoolean("IsStartTracking");

Solution 2:

assuming you are creating the TrackGPS service and an Android Service, when classes bind and unBind from your service you can create a counter that keeps track of clients which are bound and, when the last client is unbound, shutdown its listener to the gps.

I'm assuming if you are registering the LocationListener then you can figure out how to unbind from the service. There is an example of how clients should use bind() and unbind() at the Service documentation

Post a Comment for "How To Unregister Listener & Stop Service From Within Broadcastreceiver"