在我的主要活动中,我将此标志设置为“创建:
”this.getWindow()addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
但我希望能够在BroadcastReceiver上关闭它
android.intent.action.ACTION_BATTERY_LOW"
我该怎么做?我的主应用程序将如何知道已收到广播并且应该关闭此标志? 。
答案 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);
}