简体   繁体   中英

android Programmatically set screen brightness and keep it

I'm trying to set the screen brightness from my app, but as soon the screen rotates (Auto-Rotate) my brightness is reset to the systems default brightness.

The code I'm using is following:

final WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
lp.screenBrightness = 0.5f;
((Activity) context).getWindow().setAttributes(lp);
((Activity) context).startActivity(new Intent(context, DummyActivity.class));

This is happening because your activity is restarting. You can try adding your window settings code in onCreate of your activity. Make sure that this code is added before setting the view of the activity.

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     final WindowManager.LayoutParams lp = getWindow().getAttributes();
     lp.screenBrightness = 0.5f;
     getWindow().setAttributes(lp);

     setContentView(R.layout.your_layout_id);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM