简体   繁体   English

android以编程方式设置屏幕亮度并保持它

[英]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. 您可以尝试在活动的onCreate中添加窗口设置代码。 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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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