开发者

Unbinding a service when Android goes to Lock mode

开发者 https://www.devze.com 2023-03-27 20:06 出处:网络
As @hackbod stated here, I used the onStart/onStop couple to bind/unbind to my service. I want my service to stop running when in lock mode (to save battery) and to resume when coming back. However,

As @hackbod stated here, I used the onStart/onStop couple to bind/unbind to my service. I want my service to stop running when in lock mode (to save battery) and to resume when coming back. However, 开发者_开发知识库Lock mode works with the onResume/onPause couple. So how do I do that?

Thanks


You can listen for broadcast when the screen turns on/off.

registerReceiver(new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // bind to service
  }
}, new IntentFilter(Intent.ACTION_SCREEN_ON));

registerReceiver(new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // unbind from service
  }
}, new IntentFilter(Intent.ACTION_SCREEN_OFF));
0

精彩评论

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

关注公众号