开发者

Stopping GPS on back button press

开发者 https://www.devze.com 2022-12-23 11:06 出处:网络
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\'v

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号