android源码中是调用IHardwareService来进行屏幕亮度调整的,但在最新的1.0r2,1.0r1 SDK中却找不到这个类。
这时又要翻出0.9的SDK了,在0.9 SDK中android.os包中有相关类。只要将这些相关类添加到项目类路径中,我们也可以用IHardwareService来调整屏幕亮度了。以下代码G1下测试通过。
/**
* 取得当前用户自定义的屏幕亮度
*/
private int getOldBrightness() {
int brightness;
try {
brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException snfe) {
brightness = 255;
}
return brightness;
}
/**
* 设置屏幕亮度
*/
private void setBrightness(int brightness) {
IHardwareService hardware = IHardwareService.Stub.asInterface(ServiceManager.getService("hardware"));
if (hardware != null) {
try {
hardware.setScreenBacklight(brightness);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
注意brightness的值为0 - 255,0最暗,255最亮。