Reputation: 12293
I implemented Android service for listening user location:
public class ListenLocationService extends Service {
IBinder mBinder = new LocalBinder();
public interface ILocationService {
Location userLocation = new Location("");
public void StartListenLocation();
public void StopListenLocation();
public Location getUserLocation();
}
LocationManager locationManager;
LocationListener locationListener;
public class LocalBinder extends Binder implements ILocationService{
public void StopListenLocation(){
//so many attempts to stop service and no one helped
locationManager.removeUpdates(locationListener);
locationListener = null;
stopSelf();
}
public void StartListenLocation()
{
locationManager = (LocationManager)ListenLocationService.this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
public void onLocationChanged(Location location) {
userLocation.set(location);
}
};
//if I have only one requestLocationUpdates situation is the same
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
400, 1, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
400, 1, locationListener);
}
public Location getUserLocation(){
return userLocation;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
I'm binding to this service in two activities. In first activity I'm just launching service:
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
mService = (ILocationService) service;
mService.StartListenLocation();
}
public void onServiceDisconnected(ComponentName arg0) {
Log.d("LOG","onServiceDisconnected");
}
};
public void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent(this, ListenLocationService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
...
}
@Override
public void onStop(){
super.onStop();
unbindService(mConnection);
mService.StopListenLocation();
}
If I launch in my app only first activity and the close it then service stops - GPS mark disappear from device screen.
However if I enter the second activity (where I'm getting userLocation) and after this close both(using Android back button), then GPS mark still on my screen!
Code from second activity:
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
mService = (ILocationService) service;
userLocation = mService.getUserLocation();
}
public void onServiceDisconnected(ComponentName arg0) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent(this, ListenLocationService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
...
}
@Override
public void onStop(){
super.onStop();
unbindService(mConnection);
mService.StopListenLocation();
}
You can see that I wrote in both onStop()
methods not only unbindService
(initially I thought that it'll be enough) but I'm calling StopListenLocation
function with this code:
locationManager.removeUpdates(locationListener);
locationListener = null;
stopSelf();
It stops service if only first activity was bound to it. However if both activities were bound, service doesn't stop after both onStop()
. I used Log.d
to get sure that all methods are called: onStop(), stopListenLocation(), onBind()
etc. The only thing is that onServiceDisconnected()
is not called ever.
I suppose situation is that my service starts another system service for listening location. I stop my service, but this system service that controls GPS continue to work despite of locationManager.removeUpdates(locationListener);
. Maybe I'm mistaken in my suggestion.
How can I stop GPS where both activities are stopped?
Upvotes: 1
Views: 1142
Reputation: 11782
BIND_AUTO_CREATE doesn't necessarily mean that the service will terminate itself when there are no connections, it just means that it's the first thing on the chopping block if the system needs resources. If the system is not resource starved, it will let your service continue to run, since, if your activity wants the service again, it is less expensive time-wise to bind to an existing instance than to create it from scratch.
In short, it will keep 'running', but don't worry about it - it's not consuming anything that is needed elsewhere.
EDIT: Also note that just because you have unbound from the service does not mean that it will interrupt any running processing tasks that the service is handling asynchronously. All that's happened is you've told the OS that you aren't interested in the results anymore. You must explicitly stop any running tasks before disconnecting from the service or that task will continue to consume CPU time
Upvotes: 2