以编程方式更改系统亮度

我想以编程方式更改系统亮度。为此,我使用以下代码:

WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = (255);
window.setAttributes(lp);

因为我听说最大值是255。

但它什么都不做。请建议任何可以改变亮度的东西。谢谢


答案 1

您可以使用以下命令:

// Variable to store brightness value
private int brightness;
// Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
// Window object, that will store a reference to the current window
private Window window;

在您的 onCreate 中写入:

// Get the content resolver
cResolver = getContentResolver();
 
// Get the current window
window = getWindow();
    
try {
    // To handle the auto
    Settings.System.putInt(
        cResolver,
        Settings.System.SCREEN_BRIGHTNESS_MODE,
        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
    );
    // Get the current system brightness
    brightness = Settings.System.getInt(
        cResolver, Settings.System.SCREEN_BRIGHTNESS
    );
} catch (SettingNotFoundException e) {
    // Throw an error case it couldn't be retrieved
    Log.e("Error", "Cannot access system brightness");
    e.printStackTrace();
}

编写代码以监视亮度变化。

然后,您可以按如下方式设置更新的亮度:

// Set the system brightness using the brightness variable value
Settings.System.putInt(
    cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness
);
// Get the current window attributes
LayoutParams layoutpars = window.getAttributes();
// Set the brightness of this window
layoutpars.screenBrightness = brightness / 255f;
// Apply attribute changes to this window
window.setAttributes(layoutpars);

清单中的权限:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

对于 API >= 23,您需要通过设置活动请求权限,如下所述:无法获取WRITE_SETTINGS权限


答案 2

我遇到了同样的问题。

两种解决方案:

在这里,亮度=因为我正在使用进度条(int) 0 to 100 range

1 解决方案

float brightness = brightness / (float)255;
WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = brightness;
        getWindow().setAttributes(lp);

2 解决方案

我只是使用虚拟活动来调用我的进度条搜索。stop

 Intent intent = new Intent(getBaseContext(), DummyBrightnessActivity.class);
                    Log.d("brightend", String.valueOf(brightness / (float)255));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is important
                    //in the next line 'brightness' should be a float number between 0.0 and 1.0
                    intent.putExtra("brightness value", brightness / (float)255); 
                    getApplication().startActivity(intent);

现在来到DummyBrightnessActivity.class

 public class DummyBrightnessActivity extends Activity{

private static final int DELAYED_MESSAGE = 1;

private Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);            
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == DELAYED_MESSAGE) {
                DummyBrightnessActivity.this.finish();
            }
            super.handleMessage(msg);
        }
    };
    Intent brightnessIntent = this.getIntent();
    float brightness = brightnessIntent.getFloatExtra("brightness value", 0);
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = brightness;
    getWindow().setAttributes(lp);

    Message message = handler.obtainMessage(DELAYED_MESSAGE);
    //this next line is very important, you need to finish your activity with slight delay
    handler.sendMessageDelayed(message,200); 
}

}

不要忘记注册DummyBrightnessActive来显现。

希望它有帮助!


推荐