How to stop location manager?
Is it possible your activity isn't being destroyed? i.e.: you hit the home button. Move your gps start/stop to onStart
and onPause
.
You should call the method removeUpdates
inside the method onPause
:
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
Log.i(TAG, "onPause, done");
}
The emulator never gets rid of the GPS icon once loaded. Hence, on an emulator, you cannot use the GPS icon as a test as to whether GPS is still running. On a device, though, the icon should vanish.
Should I use two different listeners?
I sure would. I do not know whether removeUpdates()
will remove both, or even if both requests are registered with the single listener.
I use: locationManager.removeUpdates(locationListener); Its Working
@Override
protected void onPause() {
super.onPause();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.removeUpdates(locationListener);
}