手机设置的亮度调节功能

来源:互联网 发布:dnf为什么不停网络中断 编辑:程序博客网 时间:2024/05/13 20:35

转自:http://blog.csdn.net/cys410/article/details/7346114

1、亮度设置

// 判断是否自动调节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;}// 获取当前亮度public static int getScreenBrightness(Context context){int nowBrightnessValue = 0;ContentResolver resolver = context.getContentResolver();try{nowBrightnessValue = android.provider.Settings.System.getInt(                  resolver, Settings.System.SCREEN_BRIGHTNESS);}catch (Exception e){e.printStackTrace();}return nowBrightnessValue;}// 设置当前屏幕的亮度public static void setBrightness(Activity context, int brightness){WindowManager.LayoutParams lp = context.getWindow().getAttributes();lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);context.getWindow().setAttributes(lp);}// 关闭屏幕亮度自动调节   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);}// 保存屏幕的亮度public static void saveBrightness(ContentResolver resolver, int brightness){Uri uri = android.provider.Settings.System.getUriFor("screen_brighness");Settings.System.putInt(resolver, "screen_brightness", brightness);resolver.notifyChange(uri, null);}

2、xml配置文件

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn" /></LinearLayout>

dialog.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <CheckBox        android:id="@+id/cb"        android:layout_width="220dip"        android:layout_height="wrap_content"        android:text="我叫小Q" />    <SeekBar        android:id="@+id/seek"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_margin="10dip"        拖动seekbar手动调节=""        android:max="500" /></LinearLayout>

3、Activity

private boolean ff = false;private Button btn;private SeekBar sb;private CheckBox cb;private View v1;private AlertDialog alt;private int m;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);btn = (Button)this.findViewById(R.id.btn);ff = this.getSharedPreferences("hoperun", MODE_PRIVATE).getBoolean("checkable", true);m = this.getSharedPreferences("hoperun", MODE_PRIVATE).getInt("light", BrightUtil.getScreenBrightness(this));Toast.makeText(this, m + "", 10000).show();v1 = this.mm(this);btn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){DialogActivity.this.alt =new AlertDialog.Builder(DialogActivity.this).setView(v1).setTitle("test").setIcon(R.drawable.ic_launcher).setMessage("目前亮度为" + m).setPositiveButton("ddddd", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which){Editor et = DialogActivity.this.getSharedPreferences("hoperun", MODE_PRIVATE).edit();if (ff)et.putBoolean("checkable", ff);else{et.putBoolean("checkable", ff);Toast.makeText(DialogActivity.this, m + "", 10000).show();et.putInt("light", m);}et.commit();}}).show();}});handle();}private View mm(DialogActivity DialogActivity){v1 = LayoutInflater.from(this).inflate(R.layout.dialog, null);sb = (SeekBar)v1.findViewById(R.id.seek);cb = (CheckBox)v1.findViewById(R.id.cb);return v1;}private void handle(){if (ff){if (!BrightUtil.isAutoBrightness(this.getContentResolver()))BrightUtil.startAutoBrightness(this);cb.setChecked(true);sb.setVisibility(sb.GONE);m = BrightUtil.getScreenBrightness(this);BrightUtil.setBrightness(this, m);}else{if (BrightUtil.isAutoBrightness(this.getContentResolver())){BrightUtil.stopAutoBrightness(this);}cb.setChecked(false);sb.setVisibility(sb.VISIBLE);sb.setProgress(m);sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){m = progress;BrightUtil.setBrightness(DialogActivity.this, m);alt.setMessage("目前亮度为" + m);Toast.makeText(DialogActivity.this, m + "", 10000).show();}@Overridepublic void onStartTrackingTouch(SeekBar seekBar){m = seekBar.getProgress();BrightUtil.setBrightness(DialogActivity.this, m);alt.setMessage("目前亮度为" + m);Toast.makeText(DialogActivity.this, m + "", 10000).show();}@Overridepublic void onStopTrackingTouch(SeekBar seekBar){m = seekBar.getProgress();BrightUtil.setBrightness(DialogActivity.this, m);alt.setMessage("目前亮度为" + m);Toast.makeText(DialogActivity.this, m + "", 10000).show();}});}cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked){if (cb.isChecked()){if (!BrightUtil.isAutoBrightness(DialogActivity.this.getContentResolver()))BrightUtil.startAutoBrightness(DialogActivity.this);cb.setChecked(true);sb.setVisibility(sb.GONE);alt.setMessage("目前亮度为:" + BrightUtil.getScreenBrightness(DialogActivity.this));ff = true;}else{ff = false;if (BrightUtil.isAutoBrightness(DialogActivity.this.getContentResolver())){BrightUtil.stopAutoBrightness(DialogActivity.this);}cb.setChecked(false);sb.setVisibility(sb.VISIBLE);m =DialogActivity.this.getSharedPreferences("hoperun", MODE_PRIVATE).getInt("light",BrightUtil.getScreenBrightness(DialogActivity.this));sb.setProgress(m);alt.setMessage("目前亮度为:" + m);sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){m = progress;BrightUtil.setBrightness(DialogActivity.this, m);alt.setMessage("目前亮度为" + m);Toast.makeText(DialogActivity.this,BrightUtil.getScreenBrightness(DialogActivity.this) + "",10000).show();}@Overridepublic void onStartTrackingTouch(SeekBar seekBar){m = seekBar.getProgress();BrightUtil.setBrightness(DialogActivity.this, m);alt.setMessage("目前亮度为" + m);}@Overridepublic void onStopTrackingTouch(SeekBar seekBar){m = seekBar.getProgress();BrightUtil.setBrightness(DialogActivity.this, m);alt.setMessage("目前亮度为" + m);Toast.makeText(DialogActivity.this,BrightUtil.getScreenBrightness(DialogActivity.this) + "",10000).show();}});}}});}

 

注意:

        这个还没在真机测试过,回头在手机上测试下,再把这个删掉。

 

原创粉丝点击