zoukankan      html  css  js  c++  java
  • android brightness control

    package com.smarttpapers.reader.dialog;
    
    import android.app.Activity;
    import android.app.Dialog;
    import android.content.ContentResolver;
    import android.content.Context;
    import android.net.Uri;
    import android.provider.Settings;
    import android.provider.Settings.SettingNotFoundException;
    import android.view.WindowManager;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    
    import com.smarttpapers.reader.R;
    
    public class DialogBrightness extends Dialog{
    
        private int brightnessValue;
        private boolean isAuto;
        public DialogBrightness(final Context context, int theme) {
            super(context, theme);
            setContentView(R.layout.popup_brightness);
            setTitle("Brightness");
            setCanceledOnTouchOutside(true);
            final SeekBar brightness = (SeekBar)findViewById(R.id.page_bightness);
            int nowBrightnessValue = 0;
            final ContentResolver resolver = ((Activity)context).getContentResolver();
            try {
                nowBrightnessValue = android.provider.Settings.System.getInt(
                        resolver, Settings.System.SCREEN_BRIGHTNESS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            brightness.setProgress(nowBrightnessValue);
            isAuto=isAutoBrightness(resolver);
            if(isAuto){
                stopAutoBrightness((Activity)context);
            }
            brightness.setOnSeekBarChangeListener(
                    new OnSeekBarChangeListener() {
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    saveBrightness(resolver, brightnessValue);
                }
                
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {}
                
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    brightnessValue=progress+10;
                    setBrightness((Activity)context, brightnessValue);
                }
            });
        }
        public 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 void setBrightness(Activity activity, int bright) {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.screenBrightness = Float.valueOf(bright) * (1f / 255f);
                activity.getWindow().setAttributes(lp);
            }
        public void startAutoBrightness(Activity activity) {
            Settings.System.putInt(activity.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        }
        public void stopAutoBrightness(Activity activity) {
            Settings.System.putInt(activity.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        }
        public void saveBrightness(ContentResolver resolver, int bright) {
            Uri uri = android.provider.Settings.System
                    .getUriFor("screen_brightness");
            android.provider.Settings.System.putInt(resolver, "screen_brightness",
                    bright);
            resolver.notifyChange(uri, null);
        }
    }
  • 相关阅读:
    模板的一些概念和技巧
    [转] Linux TCP/IP网络小课堂:net-tools与iproute2大比较
    [转] boost库的Singleton的实现以及static成员的初始化问题
    static对象的高级用法
    const中的一些tricky的地方
    delphi软件启动的顺序解密。
    属性名、变量名与 内部关键字 重名 加&
    delphi Inc函数和Dec函数的用法
    Centos 关闭防火墙
    IntelliJ IDEA 启动方法
  • 原文地址:https://www.cnblogs.com/qiengo/p/2501029.html
Copyright © 2011-2022 走看看