• RK Android5.1 亮度条、ContentObserver内容观察者模式


    一.Setting 里面 亮度条弹窗

    1.1.packages/apps/Settings/res/xml/display_settings.xml

            <PreferenceScreen
                    android:key="brightness"
                    android:title="@string/brightness"
                    settings:keywords="@string/keywords_display_brightness_level">
                <intent android:action="android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
            </PreferenceScreen>
    

    1.2.frameworks/base/packages/SystemUI/AndroidManifest.xml

            <activity
                android:name=".settings.BrightnessDialog"
                android:label="@string/quick_settings_brightness_dialog_title"
                android:theme="@android:style/Theme.DeviceDefault.Dialog"
                android:finishOnCloseSystemDialogs="true"
                android:launchMode="singleInstance"
                android:excludeFromRecents="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    

    1.3.frameworksasepackagesSystemUIsrccomandroidsystemuisettingsBrightnessDialog.java  亮度条弹框

    public class BrightnessDialog extends Activity {
    
        private BrightnessController mBrightnessController;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            final Window window = getWindow();
    
            window.setGravity(Gravity.TOP);
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            window.requestFeature(Window.FEATURE_NO_TITLE);
            //布局文件
            setContentView(R.layout.quick_settings_brightness_dialog);
    
            final ImageView icon = (ImageView) findViewById(R.id.brightness_icon);
            //亮度条
            final ToggleSlider slider = (ToggleSlider) findViewById(R.id.brightness_slider);
            mBrightnessController = new BrightnessController(this, icon, slider);
        }
    

    1.4. 全场重点来了 BrightnessController 获取设备最大的亮度和最低的亮度值、设置屏幕的亮度、保存和获取当前的亮度值和亮度调节模式  
    ContentObserver 内容观察者 观察(捕捉)特定Uri引起的数据库的变化 继而做一些相应的处理
    这就不用另写线程 去监听数据库的变化 更新UI

    推荐的文章  Android中内容观察者的使用---- ContentObserver类详解

    ContentObserver类介绍

     构造方法 public void ContentObserver(Handler handler)  
           说明:所有   ContentObserver的派生类都需要调用该构造方法
        参数: handler  Handler对象。可以是主线程Handler(这时候可以更新UI 了),也可以是任何Handler对象。
    
     常用方法
       void onChange(boolean selfChange)
           功能:当观察到的Uri发生变化时,回调该方法去处理。所有ContentObserver的派生类都需要重载该方法去处理逻辑。
           参数:selfChange
    

    frameworksasepackagesSystemUIsrccomandroidsystemuisettingsBrightnessController.java  

    ContentObserver to watch brightness   内部类 BrightnessObserver

    onChange 更新亮度条

        private class BrightnessObserver extends ContentObserver {
    
            private final Uri BRIGHTNESS_MODE_URI =
                    Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE);
            private final Uri BRIGHTNESS_URI =
                    Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
            private final Uri BRIGHTNESS_ADJ_URI =
                    Settings.System.getUriFor(Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ);
    
            public BrightnessObserver(Handler handler) {
                super(handler);
            }
    
            @Override
            public void onChange(boolean selfChange) {
                onChange(selfChange, null);
            }
    
            @Override
            public void onChange(boolean selfChange, Uri uri) {
                if (selfChange) return;
                try {
                    mExternalChange = true;
                    if (BRIGHTNESS_MODE_URI.equals(uri)) {
                        updateMode();
                        updateSlider();
                    } else if (BRIGHTNESS_URI.equals(uri) && !mAutomatic) {
                        updateSlider();
    

    Android灯光系统(3)——背光灯控制实现 刷新UI 

        public BrightnessController(Context context, ImageView icon, ToggleSlider control) {
            mContext = context;
            mIcon = icon;
            mControl = control;
            mHandler = new Handler();
    	………………………………………………
        @Override
        public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) {
            updateIcon(mAutomatic);
            if (mExternalChange) return;
    
            if (!mAutomatic) {
                final int val = value + mMinimumBacklight;
                setBrightness(val);
            /*如果当前不是一直按着滑动块而是松手了的话就把那些数据写到数据库里面去。
            注意在滑动过程中是直接调用setBrightness(),而不是通过数据库来实现的。但是松手后
            才写入数据库的。
            */
                if (!tracking) {
                    AsyncTask.execute(new Runnable() {
                            public void run() {
                                Settings.System.putIntForUser(mContext.getContentResolver(),
                                        Settings.System.SCREEN_BRIGHTNESS, val,
                                        UserHandle.USER_CURRENT);
                            }
                        });
                }
            }	
    

     二.亮度调整显示“当前亮度值xxx”

     

     

    2.1.Patch

    diff --git a/frameworks/base/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml b/frameworks/base/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
    old mode 100644
    new mode 100755
    index a996260..8e030a1
    --- a/frameworks/base/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
    +++ b/frameworks/base/packages/SystemUI/res/layout/quick_settings_brightness_dialog.xml
    @@ -18,7 +18,12 @@
         android:paddingLeft="16dp"
         android:paddingRight="16dp"
         style="@style/BrightnessDialogContainer">
    -
    +    
    +    <LinearLayout
    +    	android:layout_width="match_parent"
    +	android:layout_height="wrap_content"
    +    	android:orientation="vertical">
    +	
         <ImageView
             android:id="@+id/brightness_icon"
             android:layout_width="wrap_content"
    @@ -31,10 +36,36 @@
     
         <com.android.systemui.settings.ToggleSlider
             android:id="@+id/brightness_slider"
    -        android:layout_width="0dp"
    +        android:layout_width="wrap_content"
             android:layout_height="wrap_content"
    -        android:layout_gravity="center_vertical"
    -        android:layout_weight="1"
    +        android:layout_gravity="center"
             systemui:text="@string/status_bar_settings_auto_brightness_label" />
     
    +    <LinearLayout
    +    	android:layout_width="match_parent"
    +	android:layout_height="wrap_content"
    +    	android:orientation="horizontal"
    +	android:gravity="center">
    +	
    +    <TextView
    +        android:id="@+id/brightness_now_value"
    +        android:layout_width="wrap_content"
    +        android:layout_height="wrap_content"
    +	android:text="@string/brightness_now_value"
    +	android:textSize="16sp"
    +	android:textColor="#FFFFFF"
    +	/>
    +
    +    <TextView
    +        android:id="@+id/brightness_slider_value"
    +        android:layout_width="wrap_content"
    +        android:layout_height="wrap_content"
    +	android:textSize="16sp"
    +	android:textColor="#FFFFFF"/>
    +	
    +    </LinearLayout>
    +    	
    +    </LinearLayout>
    +    		
    +
     </LinearLayout>
    diff --git a/frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml b/frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml
    index 9cc642c..a54c4d0 100755
    --- a/frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml
    +++ b/frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml
    @@ -437,4 +437,5 @@
         <string name="power_sub_close">关闭系统</string>
         <string name="power_sub_sleep">系统休眠</string>
         <string name="power_sub_reboot">重启系统</string>
    +	<string name="brightness_now_value">当前屏幕亮度: </string>
     </resources>
    diff --git a/frameworks/base/packages/SystemUI/res/values/strings.xml b/frameworks/base/packages/SystemUI/res/values/strings.xml
    index d4d7d07..90f7b12 100755
    --- a/frameworks/base/packages/SystemUI/res/values/strings.xml
    +++ b/frameworks/base/packages/SystemUI/res/values/strings.xml
    @@ -1003,4 +1003,5 @@
         <string name="power_sub_close">Shutdown</string>
         <string name="power_sub_sleep">Go to Sleep</string>
         <string name="power_sub_reboot">Reboot</string>
    +	<string name="brightness_now_value">Current screen brightness: </string>
     </resources>
    diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
    old mode 100644
    new mode 100755
    index 4dacacf..c30625d
    --- a/frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
    +++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
    @@ -106,7 +106,8 @@ public class QSPanel extends ViewGroup {
     
             mBrightnessController = new BrightnessController(getContext(),
                     (ImageView) findViewById(R.id.brightness_icon),
    -                (ToggleSlider) findViewById(R.id.brightness_slider));
    +                (ToggleSlider) findViewById(R.id.brightness_slider),
    +				(TextView) findViewById(R.id.brightness_slider_value));
     
             mDetailDoneButton.setOnClickListener(new OnClickListener() {
                 @Override
    diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
    old mode 100644
    new mode 100755
    index 946a3d1..84cfe50
    --- a/frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
    +++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
    @@ -31,6 +31,7 @@ import android.os.UserHandle;
     import android.provider.Settings;
     import android.widget.CompoundButton;
     import android.widget.ImageView;
    +import android.widget.TextView;
     
     import com.android.systemui.R;
     
    @@ -51,6 +52,7 @@ public class BrightnessController implements ToggleSlider.Listener {
     
         private final Context mContext;
         private final ImageView mIcon;
    +	private final TextView mText_brightness;
         private final ToggleSlider mControl;
         private final boolean mAutomaticAvailable;
         private final IPowerManager mPower;
    @@ -133,10 +135,11 @@ public class BrightnessController implements ToggleSlider.Listener {
     
         }
     
    -    public BrightnessController(Context context, ImageView icon, ToggleSlider control) {
    +    public BrightnessController(Context context, ImageView icon, ToggleSlider control ,TextView text_brightness) {
             mContext = context;
             mIcon = icon;
             mControl = control;
    +		mText_brightness = text_brightness;
             mHandler = new Handler();
             mUserTracker = new CurrentUserTracker(mContext) {
                 @Override
    @@ -301,6 +304,7 @@ public class BrightnessController implements ToggleSlider.Listener {
                         UserHandle.USER_CURRENT);
                 mControl.setMax(mMaximumBacklight - mMinimumBacklight);
                 mControl.setValue(value - mMinimumBacklight);
    +			mText_brightness.setText(" "+value);
             }
         }
     
    diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
    old mode 100644
    new mode 100755
    index a1704ff..2a4a720
    --- a/frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
    +++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
    @@ -27,6 +27,7 @@ import android.view.KeyEvent;
     import android.view.Window;
     import android.view.WindowManager;
     import android.widget.ImageView;
    +import android.widget.TextView;
     
     import com.android.systemui.R;
     
    @@ -49,7 +50,8 @@ public class BrightnessDialog extends Activity {
     
             final ImageView icon = (ImageView) findViewById(R.id.brightness_icon);
             final ToggleSlider slider = (ToggleSlider) findViewById(R.id.brightness_slider);
    -        mBrightnessController = new BrightnessController(this, icon, slider);
    +		final TextView brightness_value = (TextView) findViewById(R.id.brightness_slider_value);
    +        mBrightnessController = new BrightnessController(this, icon, slider , brightness_value);
         }
     
         @Override
    

      

  • 相关阅读:
    java面向对象,封装和继承之电子宠物应用
    java之面向对象及定义一个计算器
    java之练习字符串的处理
    java编译变量的正确写法和控制台输入
    java基础类型及运算符
    JS之表单验证及职业素养
    JS实例之左侧菜单下拉效果,实现左侧菜单栏点击打开关闭效果
    JS实例之列表选中,实现类似好友列表选中效果
    idea激活码+方法 亲测有效
    恶意软件关不掉,老是弹出来
  • 原文地址:https://www.cnblogs.com/crushgirl/p/15474247.html
Copyright © 2020-2023  润新知