Public class SystemManager {private Context mContext; private static SystemManager sInstance; private SystemManager (final Context context) {mContext = context;} public static SystemManager init (final Context context) {if (null = sInstance) {sInstance = new SystemManager (context);} return sInstance;} public static SystemManager getInstance () {return sInstance ;} // determine whether the automatic dimming mode is public boolean isAutoBrightness () {boolean automicBrightness = false; try {ContentResolver resolver = mContext. getContentResolver (); automicBrightness = Settings. system. getInt (resolver, Settings. system. SCREEN_BRIGHTNESS_MODE) = Settings. system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC;} catch (SettingNotFoundException e) {e. printStackTrace ();} return automicBrightness;} // set the screen brightness public void setBrightness (Activity activity, int brightness) {WindowManager. layoutParams lp = activity. getWindow (). getAttributes (); lp. screenBrightness = Float. valueOf (brightness) * (1f/255f); activity. getWindow (). setAttributes (lp);} // Save the screen brightness public void saveBrightness (int brightness) {ContentResolver resolver = mContext. getContentResolver (); Uri uri = android. provider. settings. system. getUriFor ("screen_brightness"); android. provider. settings. system. putInt (resolver, "screen_brightness", brightness); resolver. policychange (uri, null);} // Enable Automatic dimming mode public void startAutoBrightness () {ContentResolver resolver = mContext. getContentResolver (); Settings. system. putInt (resolver, Settings. system. SCREEN_BRIGHTNESS_MODE, Settings. system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC); Uri uri = android. provider. settings. system. getUriFor ("screen_brightness"); resolver. policychange (uri, null);} // disable the automatic dimming mode public void stopAutoBrightness () {ContentResolver resolver = mContext. getContentResolver (); Settings. system. putInt (resolver, Settings. system. SCREEN_BRIGHTNESS_MODE, Settings. system. SCREEN_BRIGHTNESS_MODE_MANUAL); Uri uri = android. provider. settings. system. getUriFor ("screen_brightness"); resolver. policychange (uri, null);} // obtain the current screen brightness. public int getScreenBrightness () {int nowBrightnessValue = 0; try {ContentResolver resolver = mContext. getContentResolver (); nowBrightnessValue = android. provider. settings. system. getInt (resolver, Settings. system. SCREEN_BRIGHTNESS);} catch (Exception e) {e. printStackTrace ();} return nowBrightnessValue;} // set public void setBrightnessMode (int mode) {Settings. system. putInt (mContext. getContentResolver (), Settings. system. SCREEN_BRIGHTNESS_MODE, mode);} // obtain the brightness mode public int getBrightnessMode () {try {return Settings. system. getInt (mContext. getContentResolver (), Settings. system. SCREEN_BRIGHTNESS_MODE);} catch (SettingNotFoundException e) {return Settings. system. SCREEN_BRIGHTNESS_MODE_AUTOMATIC ;}}}
Permission
<Uses-permission android: name = "android. permission. WRITE_SETTINGS"/>