更改编程系统亮度

更改编程系统亮度

本文介绍了更改编程系统亮度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式更改系统的亮度。为此我用这code:

i want to change the system brightness programmatically. for that purpose i am using this code:

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

因为我听说,最大值为255。

because i heard that max value is 255.

,但它确实nothing.please提出可改变亮度的任何事情。谢谢

but it does nothing.please suggest any thing that can change the brightness.Thanks

推荐答案

您可以使用下列内容:

//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写:

In your onCreate write:

//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 = 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();
            }

写code监测亮度的变化。

Write the code to monitor the change in brightness.

然后就可以设置更新亮度如下:

then you can set the updated brightness as follows:

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

在权限清单:

Permission in manifest:

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

这篇关于更改编程系统亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:38