安卓添加虚拟按键(home键,返回键,列表键)

Platform: msm8916
OS:安卓5.1
Kernel: 3.10.49

相关文章:安卓去除虚拟按键

实现功能:1.导航栏添加声音图标(声音有两种状态,打开和关闭)

                  2.在打开状态下———点击———》图标会改变成关闭状态。

                  3.点击时同时会发送广播,通知应用。

 

          com.android.open.sound

          com.android.close.sound

 

文件修改:

xml布局文件:

 

		--- a/frameworks/base/packages/SystemUI/res/layout/navigation_bar.xml
+++ b/frameworks/base/packages/SystemUI/res/layout/navigation_bar.xml
@@ -87,7 +87,22 @@
+++ b/frameworks/base/packages/SystemUI/res/layout/navigation_bar.xml
@@ -87,7 +87,22 @@
                 android:scaleType="center"
                android:visibility="gone"
                android:contentDescription="@string/accessibility_recent"
+               />
+
+            <View 
+                android:layout_width="0dp"
+                android:layout_height="match_parent"
+                android:layout_weight="1"
+                android:visibility="invisible"
                 />
+            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/sound"
+                android:layout_width="@dimen/navigation_key_width"
+                android:layout_height="match_parent"
+                android:src="@drawable/ic_sysbar_sound"
+                android:layout_weight="0"
+                android:scaleType="center"
+               android:contentDescription="@string/accessibility_sound"
+               />
             <FrameLayout
                 android:layout_width="@dimen/navigation_side_padding"
                 android:layout_height="match_parent"
@@ -232,7 +247,22 @@
                 android:layout_width="match_parent"
                 android:layout_weight="1"
                 android:visibility="invisible"
+               />
+               
+           <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/sound"
+                android:layout_height="@dimen/navigation_key_width"
+                android:layout_width="match_parent"
+                android:src="@drawable/ic_sysbar_sound_land"
+                android:scaleType="center"
+                android:layout_weight="0"
+               android:contentDescription="@string/accessibility_sound"
                 />
+            <View 
+                android:layout_height="match_parent"
+                android:layout_width="match_parent"
+                android:layout_weight="1"
+                android:visibility="invisible"
+               />
             <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/home"

 

--- a/frameworks/base/packages/SystemUI/res/layout-sw600dp/navigation_bar.xml
+++ b/frameworks/base/packages/SystemUI/res/layout-sw600dp/navigation_bar.xml
@@ -80,7 +80,15 @@
                android:layout_weight="0"
                android:visibility="gone"
                 android:contentDescription="@string/accessibility_recent"
-                />
+               />
+            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/sound"
+                android:layout_width="128dp" android:paddingStart="25dp" android:paddingEnd="25dp"
+                android:layout_height="match_parent"
+                android:src="@drawable/ic_sysbar_sound"
+                android:scaleType="centerInside"
+               android:layout_weight="0"
+                android:contentDescription="@string/accessibility_sound"
+               />
             <Space 
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
@@ -226,7 +234,15 @@
                android:layout_weight="0"
                android:visibility="gone"
                 android:contentDescription="@string/accessibility_recent"
-                />
+               />
+            <com.android.systemui.statusbar.policy.KeyButtonView android:id="@+id/sound"
+                android:layout_width="162dp" android:paddingStart="42dp" android:paddingEnd="42dp"
+                android:layout_height="match_parent"
+                android:src="@drawable/ic_sysbar_sound"
+                android:scaleType="centerInside"
+               android:layout_weight="0"
+                android:contentDescription="@string/accessibility_sound"
+               />
             <Space 
--- a/frameworks/base/packages/SystemUI/res/values/strings.xml
+++ b/frameworks/base/packages/SystemUI/res/values/strings.xml
@@ -204,6 +204,8 @@
     <string name="accessibility_menu">Menu</string>
     <!-- Content description of the recents button for accessibility (not shown on the screen). [CHAR LIMIT=NONE] -->
     <string name="accessibility_recent">Overview</string>
+    <string name="accessibility_sound">Sound</string>
+    <string name="accessibility_nosound">Nosound</string>
--- a/frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml
+++ b/frameworks/base/packages/SystemUI/res/values-zh-rCN/strings.xml
@@ -85,6 +85,8 @@
     <string name="accessibility_home" msgid="8217216074895377641">"主屏幕"</string>
     <string name="accessibility_menu" msgid="316839303324695949">"菜单"</string>
     <string name="accessibility_recent" msgid="5208608566793607626">"概览"</string>
+    <string name="accessibility_sound" msgid="5208608566793607777">"打开声音"</string>
+    <string name="accessibility_nosound" msgid="5208608566793607888">"关闭声音"</string>

java代码的修改:

--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
@@ -45,6 +45,13 @@ import android.view.inputmethod.InputMethodManager;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
+import android.content.BroadcastReceiver;
+import android.widget.Toast; 
+import android.os.IBinder;
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.content.Intent;
+import android.content.IntentFilter;
 
 import com.android.systemui.R;
 import com.android.systemui.statusbar.BaseStatusBar;
@@ -90,7 +97,7 @@ public class NavigationBarView extends LinearLayout {
 
     // performs manual animation in sync with layout transitions
     private final NavTransitionListener mTransitionListener = new NavTransitionListener();
-
+    AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
     private OnVerticalChangedListener mOnVerticalChangedListener;
     private boolean mIsLayoutRtl;
     private boolean mDelegateIntercepted;
@@ -255,6 +262,40 @@ public class NavigationBarView extends LinearLayout {
         return mCurrentView.findViewById(R.id.recent_apps);
     }
 
+       //add by zk
+       public View getAddVolume(){  
+       
+                IntentFilter mfilter = new IntentFilter( );
+         mfilter.addAction("com.android.open.audio");
+         mfilter.addAction("com.android.close.audio");
+                mContext.registerReceiver( mReceiver, mfilter );
+                
+
+               ImageView mView = (ImageView)mCurrentView.findViewById(R.id.sound);             
+               
+       
+       
+       return mView;  
+    }  
+       BroadcastReceiver mReceiver = new BroadcastReceiver( ) {//接收应用发送过来的广播,保存了关机前的状态。
+                @Override
+                public void onReceive( Context context, Intent intent ) {
+                        String action = intent.getAction( );
+                        Log.e(TAG, "action ======= " + action);
+                                               ImageView mView = (ImageView)mCurrentView.findViewById(R.id.sound);     
+                        if(action.equals("com.android.open.audio"))
+                        {
+                            mView.setImageResource(R.drawable.ic_sysbar_sound);  //显示打开音量图标                
+                        }else if(action.equals("com.android.close.audio"))
+                        {
+                             mView.setImageResource(R.drawable.ic_sysbar_nosound);//显示关闭的图标
+                        }
+                }
+     };
+       
+       
+       //end
+       
     public View getMenuButton() {
         return mCurrentView.findViewById(R.id.menu);
     }
@@ -369,6 +410,7 @@ public class NavigationBarView extends LinearLayout {
         getBackButton()   .setVisibility(disableBack       ? View.INVISIBLE : View.VISIBLE);
         getHomeButton()   .setVisibility(disableHome       ? View.INVISIBLE : View.VISIBLE);
         getRecentsButton().setVisibility(disableRecent     ? View.INVISIBLE : View.VISIBLE);
+               getAddVolume().setVisibility(disableRecent     ? View.INVISIBLE : View.VISIBLE);
        getRecentsButton().setVisibility(View.GONE);
 
         mBarTransitions.applyBackButtonQuiescentAlpha(mBarTransitions.getMode(), true /*animate*/);
@@ -477,7 +519,7 @@ public class NavigationBarView extends LinearLayout {
     @Override
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
         super.onLayout(changed, l, t, r, b);
-        mDelegateHelper.setInitialTouchRegion(getHomeButton(), getBackButton(), getRecentsButton());
+        mDelegateHelper.setInitialTouchRegion(getHomeButton(), getBackButton(), getRecentsButton(),getAddVolume());
     }
 
     @Override
@@ -650,7 +692,7 @@ public class NavigationBarView extends LinearLayout {
         dumpButton(pw, "home", getHomeButton());
         dumpButton(pw, "rcnt", getRecentsButton());
         dumpButton(pw, "menu", getMenuButton());
-
+               dumpButton(pw, "menu", getAddVolume());
         pw.println("    }");
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -184,6 +184,15 @@ import com.android.systemui.statusbar.stack.StackScrollAlgorithm;
 import com.android.systemui.statusbar.stack.StackScrollState.ViewState;
 import com.android.systemui.volume.VolumeComponent;
 
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.content.Intent; 
+import android.app.Application;
+import android.view.KeyCharacterMap;
+import android.view.KeyCharacterMap.FallbackAction;
+import android.view.KeyEvent;
+
+
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.ArrayList;
@@ -211,7 +220,8 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
                       = "com.android.internal.policy.statusbar.START";
 
               public static final boolean SHOW_LOCKSCREEN_MEDIA_ARTWORK = true;
-
+                   
+                  private static int sound_flag = 0;
               private static final int MSG_OPEN_NOTIFICATION_PANEL = 1000;
               private static final int MSG_CLOSE_PANELS = 1001;
               private static final int MSG_OPEN_SETTINGS_PANEL = 1002;
@@ -643,7 +653,29 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
 
                       notifyUserAboutHiddenNotifications();
                       mScreenPinningRequest = new ScreenPinningRequest(mContext);
+                          //add by zk
+                               IntentFilter mfilter = new IntentFilter( );
+                               mfilter.addAction("com.android.open.audio");
+                               mfilter.addAction("com.android.close.audio");
+                               mContext.registerReceiver( mReceiver, mfilter );
+                          
               }//接收应用发送过来的广播,保存的是关机之前的状态。
+                  BroadcastReceiver mReceiver = new BroadcastReceiver( ) {
+                @Override
+                public void onReceive( Context context, Intent intent ) {
+                        String action = intent.getAction( );
+                        Log.e(TAG, "action ======= " + action);
+                        if(action.equals("com.android.open.audio"))
+                        {
+                            sound_flag = 1;                 
+                        }else if(action.equals("com.android.close.audio"))
+                        {
+                            sound_flag = 0;
+                        }
+                }
+        };
+        
+                  
 
               boolean isMSim() {
                       return (TelephonyManager.getDefault().getPhoneCount() > 1);
@@ -1299,6 +1331,7 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
                     mNavigationBarView.getBackButton().setLongClickable(true);
                     mNavigationBarView.getBackButton().setOnLongClickListener(mLongPressBackRecentsListener);
                     mNavigationBarView.getHomeButton().setOnTouchListener(mHomeActionListener);
                    //监听那个按键被按下。
+                    mNavigationBarView.getAddVolume().setOnClickListener(mAddVolumeClickListener);
                     mNavigationBarView.getRecentsButton().setVisibility(View.GONE);
                     updateSearchPanel();
                       }
@@ -2844,6 +2877,37 @@ public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
                               }
                               checkBarModes();
                       }
+                          
+       private boolean flag=false; 
+       private View.OnClickListener mAddVolumeClickListener = new View.OnClickListener() {
+               @Override  
+        public void onClick(View view) {  
+                
+          
+                       ImageView mView = (ImageView)view;
+                       
+                       if(sound_flag == 0){//根据标志位判断显示的状态。
+                                       //unmute
+                                       Log.d(TAG, "sendBroadcast:com.android.open.sound");
+                                       mView.setImageResource(R.drawable.ic_sysbar_sound);
+                                       Intent intent = new Intent();
+                    			intent.setAction("com.android.open.sound");//向应用发送广播。
+                    			mContext.sendBroadcast(intent);
+                                       sound_flag = 1;
+                                       
+                                                 
+                               }else {
+                                       //mute
+                                       Log.d(TAG, "sendBroadcast:com.android.close.sound");
+                                       mView.setImageResource(R.drawable.ic_sysbar_nosound);   
+                                       Intent intent = new Intent();
+                    intent.setAction("com.android.close.sound");//向应用发送广播。
+                    mContext.sendBroadcast(intent);
+                                       sound_flag = 0;
+                               }
+        }  
+    };  
 
			intent.setAction("com.android.open.sound");//向应用发送广播。
+                    			mContext.sendBroadcast(intent);
+                                       sound_flag = 1;
+                                       
+                                                 
+                               }else {
+                                       //mute
+                                       Log.d(TAG, "sendBroadcast:com.android.close.sound");
+                                       mView.setImageResource(R.drawable.ic_sysbar_nosound);   
+                                       Intent intent = new Intent();
+                    intent.setAction("com.android.close.sound");//向应用发送广播。
+                    mContext.sendBroadcast(intent);
+                                       sound_flag = 0;
+                               }
+        }  
+    };  
 
 

图片资源目录:

#       frameworks/base/packages/SystemUI/res/drawable-hdpi/

#       frameworks/base/packages/SystemUI/res/drawable-mdpi/

#       frameworks/base/packages/SystemUI/res/drawable-xhdpi/

#       frameworks/base/packages/SystemUI/res/drawable-xxhdpi

 

居中问题patch文件:https://download.csdn.net/download/kai_zone/10576998

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kevin@1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值