1. 程式人生 > >Android4.2.2 動態顯示隱藏屏幕底部的導航欄(對系統源碼進行修改)

Android4.2.2 動態顯示隱藏屏幕底部的導航欄(對系統源碼進行修改)

留下 public side equals android4 init bold 方法 equal

需求如題。

在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 ==
    null) return;
  4. prepareNavigationBarView();
  5. mWindowManager.addView(mNavigationBarView, getNavigationBarLayoutParams());
  6. }

<2>隱藏方法定義如下(新加):

[java] view plain copy
  1. private void removeNavigationBar() {
  2. if (mNavigationBarView == null) return;
  3. mWindowManager.removeView(mNavigationBarView);
  4. 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. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. String action = intent.getAction();
  5. if (isOrderedBroadcast()) {
  6. if (action.equals("MyRecv_Action")) {
  7. String cmd = intent.getStringExtra("cmd");
  8. //布爾標誌isDisplayNavBar保存當前導航欄的狀態
  9. if(cmd.equals("hide")&&isDisplayNavBar){
  10. isDisplayNavBar=false;
  11. removeNavigationBar();
  12. }else if(cmd.equals("show")&&!isDisplayNavBar){
  13. addNavigationBar();
  14. isDisplayNavBar=true;
  15. }
  16. }
  17. this.abortBroadcast();
  18. }
  19. }
  20. ;

至此修改完畢,編譯完畢之後產生新的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);

Android4.2.2 動態顯示隱藏屏幕底部的導航欄(對系統源碼進行修改)