您的位置:首页 > 其它

如何调整系统屏幕亮度和程序内部屏幕亮度

2012-11-30 14:37 260 查看
在使用系统的IPowerManager设置系统屏幕亮度,代码如下(使用这个代码需要导入外部jar包,Y:\w990settings\alps\out\target\common\obj\JAVA_LIBRARIES\framework_intermediates\classes.jar):
    private void setBrightness(int brightness) {

        try {

            IPowerManager power = IPowerManager.Stub.asInterface(

                    ServiceManager.getService("power"));

            //Only set backlight value when screen is on

            if (power != null && power.isScreenOn()) {

                power.setBacklightBrightness(brightness);

            }

        } catch (RemoteException doe) {

            

        }

    }

该代码在settings模块,显然是android:sharedUserId="android.uid.system"(Androidmanifest.xml里面)。是因为这个原因所以能够访问。需要权限android.permission.DEVICE_POWER才能访问,并且该权限只限于使用系统固件签名的应用才能够授予,基本上只有一些系统应用才能够有该权限。所以权限问题导致了不能够授予给三方应用该权限。

但是360省电确能够调整屏幕亮度。看来还有其他方法。发现如下方法可行:

public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
tv_brightnessPercent.setText((int)((progress/255.0) * 100) + "%");
int tmpInt = sb_modBrightness.getProgress();
Settings.System.putInt(XEnergyActivity.this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, tmpInt);
tmpInt = Settings.System.getInt(XEnergyActivity.this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, -1);
WindowManager.LayoutParams wl = getWindow().getAttributes();
float tmpFloat = (float) (tmpInt/255.0);
if(tmpFloat > 0 && tmpFloat <= 1)
{
wl.screenBrightness = tmpFloat;
}
getWindow().setAttributes(wl);
}

先将亮度信息tmpInt(最大255)存入Settings.System.SCREEN_BRIGHTNESS中:

Settings.System.putInt(XEnergyActivity.this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, tmpInt);


再调整当前窗口的亮度信息:

WindowManager.LayoutParams wl = getWindow().getAttributes();
float tmpFloat = (float) (tmpInt/255.0);
if(tmpFloat > 0 && tmpFloat <= 1)
{
wl.screenBrightness = tmpFloat;
}
getWindow().setAttributes(wl);
注意这里面的w1为float类型,最大值为1.0f。所以需要进行转换。如果没有下面这个调整当前活动屏幕亮度,需要开启关闭屏幕一次,上面的那个putInt函数才会起作用。

如果只需要调整程序内部屏幕亮度(比如一些阅读器)只需要后面的WindowManager设置即可。但是,当退出程序的时候,屏幕会恢复到系统本身设置的亮度。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: