开发者

Unbinding a service when Android goes to Lock mode

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜