亮度工具类
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceManager;
import android.provider.Settings;
/**
* @author L
*/
public class BrightnessManager {
/**
* 获得当前屏幕亮度的模式
*
* @return 1 为自动调节屏幕亮度,0 为手动调节屏幕亮度,-1 获取失败
*/
public static int getScreenMode(Context context) {
int mode = -1;
try {
mode = Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return mode;
}
/**
* 获得当前屏幕亮度值
*
* @return 0--255
*/
public static int getScreenBrightness(Context context) {
int screenBrightness = -1;
try {
screenBrightness = Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return screenBrightness;
}
public static int saveBrightnessToSP(Context context) {
int screenBrightness = -1;
try {
screenBrightness = Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.edit().putInt("system_screen_bright",screenBrightness).apply();
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return screenBrightness;
}
public static void restoreBrightness(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
int paramInt = sp.getInt("system_screen_bright",90);
if(Build.VERSION.SDK_INT<23){
if (Build.VERSION.SDK_INT>=21){
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, paramInt);
Uri uri = Settings.System.getUriFor("screen_brightness");
context.getContentResolver().notifyChange(uri, null);
}
/* else {
适配4.0
}*/
}
if (Build.VERSION.SDK_INT>=23){
if (Settings.System.canWrite(context)) {
saveBrightness(context.getContentResolver(),paramInt);
}
}
}
/**
* 设置当前屏幕亮度的模式
*
* @param mode 1 为自动调节屏幕亮度,0 为手动调节屏幕亮度
*/
public static void setScreenMode(int mode, Context context) {
try {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
Uri uri = Settings.System.getUriFor("screen_brightness_mode");
context.getContentResolver().notifyChange(uri, null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 保存当前的屏幕亮度值,并使之生效
*
* @param paramInt 0-255
*/
public static void setScreenBrightness(int paramInt, Context context) {
if(Build.VERSION.SDK_INT<23){
if (Build.VERSION.SDK_INT>=21){
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, paramInt);
Uri uri = Settings.System.getUriFor("screen_brightness");
context.getContentResolver().notifyChange(uri, null);
}
/* else {
适配4.0
}*/
}
if (Build.VERSION.SDK_INT>=23){
if (Settings.System.canWrite(context)) {
saveBrightness(context.getContentResolver(),paramInt);
}
}
}
public static void saveBrightness(ContentResolver resolver, int brightness) {
//改变系统的亮度值
//这里需要权限android.permission.WRITE_SETTINGS
//设置为手动调节模式
Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
//保存到系统中
Uri uri = android.provider.Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
android.provider.Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
resolver.notifyChange(uri, null);
}
}
网友评论