开发者

Trying to set screen brightness on android phone

开发者 https://www.devze.com 2023-04-07 06:48 出处:网络
I am trying to set the screen brightness but when I try and get the current window with this.getWindow() I开发者_C百科 get null. Why is this? I will post all of my code in my setBrightness() method.

I am trying to set the screen brightness but when I try and get the current window with this.getWindow() I开发者_C百科 get null. Why is this? I will post all of my code in my setBrightness() method.

System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS,
            brightness);
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness / (float) 255;
window.setAttributes(lp);


Don't use System.putInt()... You already set the screenBrightness in the lp!

The following code works:

Window window = getWindow();
float brightness = 100.0f;
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness/255.0f;
window.setAttributes(lp);


If you are using a seekbar, try these lines,

System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
window.setAttributes(layoutpars);


If you call it from fragment add getActivity() before getWindow() like this

getActivity().getWindow().setAttributes(layoutParams);

And if use seekbar dont let your brogress be 0, cause it can make your screen goes completly dark(on android 2.3)

0

精彩评论

暂无评论...
验证码 换一张
取 消