Android 播放器中修改系统亮度
/** * 播放器Activity */ public class PlayerActivity extends Activity { /** * 系统是否自动调节亮度 */ private boolean isAutoBrightness = false; /** * 系统亮度变化值 */ private static final int SYSTEM_BRIGHTNESS_CHANGE_VALUES = 25; /** * 最大系统音量值 */ private static final int MAX_SYSTEM_BRIGHTNESS_VALUES = 255; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * 打开播放器界面判断系统是否设置了自动亮度 * 设置系统亮度时如果系统亮度设置了自动亮度,需要先关闭自动亮度 */ isAutoBrightness = DeviceUtil.isAutoBrightness(this); if (isAutoBrightness) { DeviceUtil.stopAutoBrightness(this); } } @Override protected void onResume() { /* * 播放器请求屏幕常亮需要写在onResume方法中,如果写在onCreate()方法中, * 用户在播放时按HOME后,再次回到播放器,屏幕常亮失败 */ DeviceUtil.requireScreenOn(this); /** * 解决用户按HOME后回到播放器亮度模式为自动亮度 重新方法:(自动亮度打开PlayerActivity--> 按HOME --> (进入系统设置此时 * 系统亮度为非自动亮度)修改为自动亮度 --> 回到播放器) */ isAutoBrightness = DeviceUtil.isAutoBrightness(this); if (isAutoBrightness) { DeviceUtil.stopAutoBrightness(this); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub int currSettingBrighiness = DeviceUtil.getSystemScreenBrightness(this); switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: // 系统亮度加 addSystemBrighiness(currSettingBrighiness); break; case KeyEvent.KEYCODE_DPAD_DOWN: // 系统亮度减 minusSystemBrighiness(currSettingBrighiness); break; default: break; } return super.onKeyDown(keyCode, event); } /** * 系统亮度加 * * @param currSettingBrighiness * 系统当前亮度 */ private void addSystemBrighiness(int currSettingBrighiness) { if (currSettingBrighiness < MAX_SYSTEM_BRIGHTNESS_VALUES && (currSettingBrighiness + SYSTEM_BRIGHTNESS_CHANGE_VALUES) < MAX_SYSTEM_BRIGHTNESS_VALUES) { setSystemBrighiness = currSettingBrighiness + SYSTEM_BRIGHTNESS_CHANGE_VALUES; DeviceUtil.setSystemScreenBrightness(this, setSystemBrighiness); } } /** * 系统亮度减 * * @param currSettingBrighiness * 系统当前亮度 */ public void minusSystemBrighiness(int currSettingBrighiness) { if (currSettingBrighiness >= SYSTEM_BRIGHTNESS_CHANGE_VALUES) { setSystemBrighiness = currSettingBrighiness - SYSTEM_BRIGHTNESS_CHANGE_VALUES; DeviceUtil.setSystemScreenBrightness(this, setSystemBrighiness); } } }
/** * DeviceUtil 工具类 */ public class DeviceUtil { /** * 获取系统亮度 * 取值在(0 -- 255)之间 */ public static int getSystemScreenBrightness(Context context) { int values = 0; try { values = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); } catch (SettingNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return values; } /** * 设置系统亮度 * @param systemBrightness 返回的亮度值是处于0-255之间的整型数值 */ public static boolean setSystemScreenBrightness(Context context, int systemBrightness) { return Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,systemBrightness); } /** * 系统是否自动调节亮度 * return true 是自动调节亮度 return false 不是自动调节亮度 */ public static boolean isAutoBrightness(Activity activity){ int autoBrightness = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; try { autoBrightness = Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); } catch (SettingNotFoundException e) { e.printStackTrace(); } if (autoBrightness == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) { return true; } else { return false; } } /** * 关闭系统自动调节亮度 */ public static void stopAutoBrightness(Activity activity){ Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); } /** * 打开系统自动调节亮度 */ public static void startAutoBrightness(Activity activity) { Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); } /** * 请求屏幕常亮 * @param activity */ public static void requireScreenOn(Activity activity){ activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } /** * 取消屏幕常亮 * @param activity */ public static void releaseScreenOn(Activity activity){ activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-06 22:12:13