Stopping GPS on back button press
My applic开发者_如何学Pythonation uses GPS updates while it is running and I would like that to stop when the user back out of the app as I assume it will continue wasting a lot of battery power. I've tried intercepting the back button press but it doesn't seem to work. I'm not sure if this is because it's not executing the code or I'm using the wrong command. Any help would be appreciated.
public boolean OnKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
lm.removeUpdates(this);
return true;
}
return super.onKeyDown(keyCode, event);
}
Just call removeUpdates()
in onPause()
or onStop()
, probably the latter.
精彩评论