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);  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 Android 動態隱藏顯示導航欄,狀態欄 原:android4.2.2藍牙源碼閱讀--bluedroid部分 Android 修改底部導航欄navigationbar的顏色 Android判斷屏幕上導航欄是否顯示 android 獲取屏幕底部虛擬導航欄高度 Android--底部導航欄的動態替換方案 針對使用網頁嵌套