Android4.2.2 动态显示隐藏屏幕底部的导航栏(对系统源码进行修改)

需求如题。

在Android4.2.2中,导航栏(也就是屏幕底部的三个按钮,home,back,recentapp)是系统应用SystemUi.apk的一部分,简言之,我们的需求就是让我们的app来控制SystemUi.apk,达到动态显示隐藏屏幕底部导航栏的效果。我们可以在SystemUi.apk的源码中留下接口便于我们控制导航栏的显示和隐藏,我们可以通过广播的接收与发送的方式来实现这个接口。


        app------->发送广播(hide/show)

        SystemUi.apk   ------>监听广播 (hide-隐藏导航栏,show-显示导航栏) 

SystemUi.apk是系统应用,它在Android文件系统中的路径是:/system/app/;它在android源码中的路径是:frameworks/base/packages/SystemUI/;

我们只需修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.Java

<1>显示方法使用addNavigationBar()(原有):

[java] view plain copy
  1. private void addNavigationBar() {  
  2.     if (DEBUG) Slog.v(TAG, "addNavigationBar: about to add " + mNavigationBarView);  
  3.     if (mNavigationBarView == nullreturn;  
  4.   
  5.     prepareNavigationBarView();  
  6.   
  7.     mWindowManager.addView(mNavigationBarView, getNavigationBarLayoutParams());  
  8. }  

<2>隐藏方法定义如下(新加):

[java] view plain copy
  1.  private void removeNavigationBar() {  
  2.      if (mNavigationBarView == nullreturn;   
  3. mWindowManager.removeView(mNavigationBarView);  
  4.   
  5. sp;}  
 <3>广播的注册

[java] view plain copy
  1. IntentFilter filter1 = new IntentFilter();  
  2. filter1.addAction("MyRecv_action");  
  3. context.registerReceiver(mBroadcastReceiver1, filter1);  

<4>广播监听及处理

[java] view plain copy
  1.    private BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {  
  2.   
  3. @Override  
  4. public void onReceive(Context context, Intent intent) {  
  5.   
  6.     String action = intent.getAction();  
  7.     if (isOrderedBroadcast()) {  
  8.         if (action.equals("MyRecv_Action")) {  
  9.             String cmd = intent.getStringExtra("cmd");  
  10.                 //布尔标志isDisplayNavBar保存当前导航栏的状态  
  11.             if(cmd.equals("hide")&&isDisplayNavBar){  
  12.             isDisplayNavBar=false;  
  13.                 removeNavigationBar();  
  14.             }else if(cmd.equals("show")&&!isDisplayNavBar){  
  15.                 addNavigationBar();  
  16.             isDisplayNavBar=true;  
  17.                 }  
  18.     }  
  19.         this.abortBroadcast();  
  20.     }   
  21.   
  22. }  
  23.   
  24. ;  

至此修改完毕,编译完毕之后产生新的SystemUi.apk ,替换原文件系统的SystemUi.apk 后重启即可。

在我们的app里面,如果想要隐藏导航栏:

[java] view plain copy
  1. Intent intent=new Intent();  
  2. intent.setAction("MyRecv_action");  
  3. intent.putExtra("cmd","hide");  
  4. this.sendOrderedBroadcast(intent,null);  

如果想要显示导航栏:

[java] view plain copy
  1. Intent intent=new Intent();  
  2. intent.setAction("MyRecv_action");  
  3. intent.putExtra("cmd","show");  
  4. this.sendOrderedBroadcast(intent,null);  
原文地址:https://www.cnblogs.com/muhuacat/p/7457330.html