Android的屏幕亮度好像在2.1+的时候提供了自动调节的功能,所以,如果当开启自动调节功能的时候, 我们进行调节好像是没有一点作用的,这点让我很是无语,结果只有进行判断,看是否开启了屏幕亮度的自动调节功能。
/**
* 判断是否开启了自动亮度调节
*
* @param aContext
* @return
*/
public static boolean isAutoBrightness(ContentResolver aContentResolver) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt(aContentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return automicBrightness;
}
然后就是要觉得当前的亮度了,这个就比较纠结了:
/**
* 获取屏幕的亮度
*
* @param activity
* @return
*/
public static int getScreenBrightness(Activity activity) {
int nowBrightnessValue = 0;
ContentResolver resolver = activity.getContentResolver();
try {
nowBrightnessValue = android.provider.Settings.System.getInt(
resolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
e.printStackTrace();
}
return nowBrightnessValue;
}
那如何修改屏幕的亮度呢?
/**
* 设置亮度
*
* @param activity
* @param brightness
*/
public static void setBrightness(Activity activity, int brightness) {
// Settings.System.putInt(activity.getContentResolver(),
// Settings.System.SCREEN_BRIGHTNESS_MODE,
// Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
activity.getWindow().setAttributes(lp);
}
那么,能设置了,但是为什么还是会出现,设置了,没反映呢?
嘿嘿,那是因为,开启了自动调节功能了,那如何关闭呢?这才是最重要的:
/**
* 停止自动亮度调节
*
* @param activity
*/
public static void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
能开启,那自然应该能关闭了哟哟,那怎么关闭呢?很简单的:
/**
* 开启亮度自动调节
*
* @param activity
*/
public static void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
至此,应该说操作亮度的差不多都有了,结束!
哎,本来认为是应该结束了,但是悲剧得是,既然像刚才那样设置的话,只能在当前的activity中有作用,一段退出的时候,会发现毫无作用,悲剧,原来是忘记了保存了。汗!
/**
* 保存亮度设置状态
*
* @param resolver
* @param brightness
*/
public static void saveBrightness(ContentResolver resolver, int brightness) {
Uri uri = android.provider.Settings.System
.getUriFor("screen_brightness");
android.provider.Settings.System.putInt(resolver, "screen_brightness",
brightness);
// resolver.registerContentObserver(uri, true, myContentObserver);
resolver.notifyChange(uri, null);
}
转自:http://www.cnblogs.com/Tiger-Dog/articles/2058033.html
以上是转到,以下是加的。
还要进行授权,否者不能用。
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
注意:授权都放在<manifest></manifest>之间。
(2)只能是Lunch的activity中可用,很纠结。
上网搜了另一种方法
引入了一个包hardware09.jar,在0.9的版本中,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();
}
}
}
/**
* 取得当前用户自定义的屏幕亮度
*/
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最亮。
(IHardwareService包,也就是那个hardware09.jar)
(3)一般性的解决方案:
在tabhost架构的程序中,引用主启动页面的属性,进行设置。
int dispalyBright = android.provider.Settings.System.getInt(getParent().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,-1);
android.provider.Settings.System
.putInt(
getParent().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
temInt);
WindowManager.LayoutParams lp = getParent().getWindow()
.getAttributes();
lp.screenBrightness = temInt / 255f;
/**
* 判断是否开启了自动亮度调节
*
* @param aContext
* @return
*/
public static boolean isAutoBrightness(ContentResolver aContentResolver) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt(aContentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return automicBrightness;
}
然后就是要觉得当前的亮度了,这个就比较纠结了:
/**
* 获取屏幕的亮度
*
* @param activity
* @return
*/
public static int getScreenBrightness(Activity activity) {
int nowBrightnessValue = 0;
ContentResolver resolver = activity.getContentResolver();
try {
nowBrightnessValue = android.provider.Settings.System.getInt(
resolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
e.printStackTrace();
}
return nowBrightnessValue;
}
那如何修改屏幕的亮度呢?
/**
* 设置亮度
*
* @param activity
* @param brightness
*/
public static void setBrightness(Activity activity, int brightness) {
// Settings.System.putInt(activity.getContentResolver(),
// Settings.System.SCREEN_BRIGHTNESS_MODE,
// Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
activity.getWindow().setAttributes(lp);
}
那么,能设置了,但是为什么还是会出现,设置了,没反映呢?
嘿嘿,那是因为,开启了自动调节功能了,那如何关闭呢?这才是最重要的:
/**
* 停止自动亮度调节
*
* @param activity
*/
public static void stopAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
能开启,那自然应该能关闭了哟哟,那怎么关闭呢?很简单的:
/**
* 开启亮度自动调节
*
* @param activity
*/
public static void startAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
至此,应该说操作亮度的差不多都有了,结束!
哎,本来认为是应该结束了,但是悲剧得是,既然像刚才那样设置的话,只能在当前的activity中有作用,一段退出的时候,会发现毫无作用,悲剧,原来是忘记了保存了。汗!
/**
* 保存亮度设置状态
*
* @param resolver
* @param brightness
*/
public static void saveBrightness(ContentResolver resolver, int brightness) {
Uri uri = android.provider.Settings.System
.getUriFor("screen_brightness");
android.provider.Settings.System.putInt(resolver, "screen_brightness",
brightness);
// resolver.registerContentObserver(uri, true, myContentObserver);
resolver.notifyChange(uri, null);
}
转自:http://www.cnblogs.com/Tiger-Dog/articles/2058033.html
以上是转到,以下是加的。
还要进行授权,否者不能用。
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
注意:授权都放在<manifest></manifest>之间。
(2)只能是Lunch的activity中可用,很纠结。
上网搜了另一种方法
引入了一个包hardware09.jar,在0.9的版本中,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();
}
}
}
/**
* 取得当前用户自定义的屏幕亮度
*/
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最亮。
(IHardwareService包,也就是那个hardware09.jar)
(3)一般性的解决方案:
在tabhost架构的程序中,引用主启动页面的属性,进行设置。
int dispalyBright = android.provider.Settings.System.getInt(getParent().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,-1);
android.provider.Settings.System
.putInt(
getParent().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
temInt);
WindowManager.LayoutParams lp = getParent().getWindow()
.getAttributes();
lp.screenBrightness = temInt / 255f;