zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android SystemUI源代码分析和修改

Android 分析 修改 源代码 SystemUI
2023-09-14 09:08:09 时间

1.在导航栏中添加音量加减button

一些Android音量调节button。或者从保护实体按键的角度考虑,就须要在导航栏的虚拟按键中加入音量加减调节按键。

效果例如以下图所看到的:


实现步骤例如以下:

1.首先在SystemUI中加入音量加减的资源文件。路径例如以下:

frameworks/base/packages/SystemUI/res/

将图片放入相应的drawable目录,包含音量+。和音量-,见上图。


2.改动导航栏的布局文件。路径:

frameworks/base/packages/SystemUI/res/

在相应的layout目录中找到navigation_bar.xml文件进行改动:

在返回键前面加入“音量减”。返回键的布局:

<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/back"
                android:layout_width="128dp" android:paddingStart="25dp" android:paddingEnd="25dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_back"
                systemui:keyCode="4"
                android:layout_weight="0"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_back"
                />
音量减的布局例如以下。这里先把Visibility定义为Gone,然后在代码中控制是否显示:

<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/sub"
                android:src="@drawable/sub_normal"
                android:layout_width="@dimen/navigation_key_width"
                android:layout_height="match_parent"
                android:layout_weight="0"
                systemui:keyCode="302"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:visibility="gone"/>  

“音量加”加入到“近期应用”之后,近期应用的布局:

<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/recent_apps"
                android:layout_width="128dp" android:paddingStart="25dp" android:paddingEnd="25dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_sysbar_recent"
                android:layout_weight="0"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:contentDescription="@string/accessibility_recent"
                />

音量加的布局:

<com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/add"
                android:src="@drawable/add_normal"
                android:layout_width="@dimen/navigation_key_width"
                android:layout_height="match_parent"
                android:layout_weight="0"
                systemui:keyCode="301"
                systemui:glowBackground="@drawable/ic_sysbar_highlight"
                android:visibility="gone"/> 

3.接着改动代码逻辑,文件路径:

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java

在private void prepareNavigationBarView() {……}函数中加入显示音量加减的代码:

mNavigationBarView.getAddVolume().setVisibility(View.VISIBLE);
	    mNavigationBarView.getSubVolume().setVisibility(View.VISIBLE);

相应的函数getAddVolume()和getAddVolume()要在

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java

中实现:

public View getAddVolume(){
       return mCurrentView.findViewById(R.id.add);
    }

    public View getSubVolume(){
        return mCurrentView.findViewById(R.id.sub);
    } 

最后就是功能实现了。在

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java

中加入监听函数:

private View.OnTouchListener mAddVolumeOnTouchListener = new View.OnTouchListener() {
      public boolean onTouch(View v, MotionEvent ev) {
      final int action = ev.getAction();
                switch(action) {
                case MotionEvent.ACTION_DOWN:
                   is_down = true;
                   Adjust_Volume(true);
                   maddHandler.postDelayed(maddRun, ADJUST_VOLUME_DELAY * 2);
                   break;
                case MotionEvent.ACTION_MOVE:
                   is_down = true;
                   maddHandler.postDelayed(maddRun, ADJUST_VOLUME_DELAY * 2);
    //             maddHandler.removeCallbacks(maddRun);
                   break;
                case MotionEvent.ACTION_UP:
                   is_down = false;
                   maddHandler.removeCallbacks(maddRun);
                   break;
                
         } 
         return true;
      }
    };


    private View.OnTouchListener mSubVolumeOnTouchListener = new View.OnTouchListener() {
           public boolean onTouch(View v, MotionEvent ev) {
           final int action = ev.getAction();
            int x, y;
            //int mCode = ev.getAction();

                switch (action) {
                case MotionEvent.ACTION_DOWN:
                  is_down = true;
                  Adjust_Volume(false);
                  msubHandler.postDelayed(msubRun, ADJUST_VOLUME_DELAY * 2);
                  break;
                case MotionEvent.ACTION_MOVE:
                  
                  is_down = true;
                  msubHandler.postDelayed(msubRun, ADJUST_VOLUME_DELAY * 2);
                  //msubHandler.removeCallbacks(msubRun);
                  break;
                case MotionEvent.ACTION_UP:
                  is_down = false;
                  msubHandler.removeCallbacks(msubRun);
                  break;
            }
            return true;
        }
    };

    public void Adjust_Volume(boolean opition){
            AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
            if (audioManager != null) {
                //
                 // Adjust the volume in on key down since it is more
                 // responsive to the user.
                 //
                    if(opition){
                            audioManager.adjustSuggestedStreamVolume(
                            AudioManager.ADJUST_RAISE,
                           AudioManager.USE_DEFAULT_STREAM_TYPE,
                           AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                    }else{
                            audioManager.adjustSuggestedStreamVolume(
                         AudioManager.ADJUST_LOWER,
                        AudioManager.USE_DEFAULT_STREAM_TYPE,
                        AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                    }
            }
    }




转载请注明出处:周木水的CSDN博客 http://blog.csdn.net/zhoumushui



版权声明:原创文章,转载请注明出处:http://blog.csdn.net/zhoumushui