An example of this article describes the way Android is programmed to get and set activity brightness. Share to everyone for your reference, specific as follows:
This API is only suitable for 2.1 or more versions:
/** * To determine whether the automatic brightness adjustment is turned on * * @param acontext * @return/public static Boolean isautobrightness (Contentresolver acontent
Resolver) {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; /** * Get the brightness of the screen * * @param activity * @return/public static int getscreenbrightness [activity activity] {int now
Brightnessvalue = 0;
Contentresolver resolver = Activity.getcontentresolver (); try {nowbrightnessvalue = android.provider.Settings.System.getInt (resolver, Settings.System.SCREEN_BRIGHTNES
S);
catch (Exception e) {e.printstacktrace ();
return nowbrightnessvalue; /** * Set Brightness * * @param activity * @param brightness/public static void setbrightness (activity activity, int Brigh tness) {//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); /** * Stop Automatic brightness adjustment * * @param activity/public static void stopautobrightness (activity activity) {SETTINGS.SYSTEM.P Utint (Activity.getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS, Settings.System.SCREEN_BRIGHTNESS_MODE
_manual); /** * Open Brightness automatic adjustment * * @param activity/public static void startautobrightness (activity activity) {Settings.system. Putint (Activity.getcontentresolver (), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNES
S_mode_automatic); /** * Save Brightness setting status * * @param resolver * @param brightness/public static void Savebrightness (Contentresolver resolve R, int brightness) {uri uri = andRoid.provider.Settings.System. Geturifor ("screen_brightness");
Android.provider.Settings.System.putInt (resolver, "screen_brightness", brightness);
Resolver.notifychange (URI, NULL);
}
Use Seekbar for brightness control:
Sseekbar = (SeekBar) Findviewbyid (R.id.seek_bar);
Sseekbar.setonseekbarchangelistener (New Onseekbarchangelistener () {
@Override public
Void Onstoptrackingtouch (SeekBar SeekBar) {
//TODO auto-generated method stub
}
@Override public
Void Onstarttrackingtouch (SeekBar SeekBar) {
}
@Override public
void onprogresschanged (SeekBar SeekBar, int Progress,
Boolean fromuser) {
if (Progress <) {
} else {
setbrightness (test2activity.this, progress);
}
}
;
Gets the position of the current brightness
int a =getscreenbrightness (this);
Sseekbar.setprogress (a);
<seekbar
android:id= "@+id/seek_bar"
android:layout_width= "200DP"
android:layout_height= "WRAP_" Content "
android:layout_alignparentbottom=" true "
android:max=" 255 "
android:maxheight=" 8DP
" android:minheight= "8DP"
android:paddingbottom= "5DP"
android:paddingleft= "13DP"
android: paddingright= "13DP"
android:progress= "0"
/>
I hope this article will help you with the Android program.