Android: toggle screen auto brightness temporarily
I want to disable开发者_开发问答 the automatic brightness (I want to prevent the screen from being turned off), but only when my application is active (when the Activity is running).
How should I do that?
In your onResume()
, use the PowerManager
to obtain either a SCREEN_DIM_WAKE_LOCK
, a SCREEN_BRIGHT_WAKE_LOCK
or a FULL_WAKE_LOCK
. In onPause()
, release the wake lock. You will need the WAKE_LOCK
permission.
Add these to your Activity
protected void onResume() {
// Disables power-saving
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onResume();
}
public void onBackPressed() {
// Enables power-saving
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onBackPressed();
}
精彩评论