BroadcastReceiver电池更换和FLAG_KEEP_SCREEN_ON标志?

时间:2015-01-04 23:58:19

标签: android broadcastreceiver

在我的主要活动中,我将此标志设置为“创建:

  

this.getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

但我希望能够在BroadcastReceiver上关闭它

  

android.intent.action.ACTION_BATTERY_LOW"

我该怎么做?我的主应用程序将如何知道已收到广播并且应该关闭此标志? 。

1 个答案:

答案 0 :(得分:0)

您可以通过调用BroadcastReceiver中的registerReceiver()onResume()中的unregisterReceiver()以编程方式在您的活动中注册onPause()

// Placed at your activity level
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
  public void onReceive(Context context, Intent intent);
    if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction()) {
      // Remove the flag
    } else {
      // Battery is okay, add the flag
    }
}

public void onResume() {
  IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_LOW);
  // Probably also want to receive notification that the battery has gone back up
  filter.addAction(Intent.ACTION_BATTERY_OKAY);    
  registerReceiver(mBroadcastReceiver, filter);
}

public void onPause() {
  unregisterReceiver(mBroadcastReceiver);
}