判斷是否隱藏虛擬導(dǎo)航鍵、Android監(jiān)聽 NavigationBar隱藏與顯示

View decorView = getWindow().getDecorView();
       decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
           @Override
           public void onSystemUiVisibilityChange(int visibility) {
               if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
                   new ToastUtil(mContext).showToast("11111");
               }else {
                   new ToastUtil(mContext).showToast("22222");
               }
           }
       });

最優(yōu)解
 private static Handler sHandler;
    Runnable mHideRunnable = () -> {
        int flags;
        int curApiVersion = Build.VERSION.SDK_INT;
        // This work only for android 4.4+
        if (curApiVersion >= Build.VERSION_CODES.KITKAT) {
            // hide navigation bar permanently in android activity
            // touch the screen, the navigation bar will not show
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
        } else {
            // touch the screen, the navigation bar will show
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }
        // must be executed in main thread
        getWindow().getDecorView().setSystemUiVisibility(flags);
    };
 sHandler = new Handler();
        sHandler.post(mHideRunnable); // hide the navigation bar
        decorView.setOnSystemUiVisibilityChangeListener(visibility -> {
            sHandler.post(mHideRunnable); // hide the navigation bar
            if (visibility == 0) {
                lp.height = DisplayUtil.getScreenH(mContext) - titleHeight - DisplayUtil.getNavigationBarHeight(mContext);
            } else {
                lp.height = DisplayUtil.getScreenH(mContext) - titleHeight;
            }
            window.setAttributes(lp);
        });

判斷是否隱藏虛擬導(dǎo)航鍵---嘗試方法

    /**
     *
     * @param context
     * @return 返回true表示顯示虛擬導(dǎo)航鍵稍走、false表示隱藏虛擬導(dǎo)航鍵
     */
    public static boolean hasNavigationBar(Context context) {
        //navigationGestureEnabled()從設(shè)置中取不到值的話,返回false,因此也不會影響在其他手機(jī)上的判斷
        return deviceHasNavigationBar() && !navigationGestureEnabled(context);
    }
    /**
     * 獲取主流手機(jī)設(shè)置中的"navigation_gesture_on"值户侥,判斷當(dāng)前系統(tǒng)是使用導(dǎo)航鍵還是手勢導(dǎo)航操作
     * @param context app Context
     * @return
     * false 表示使用的是虛擬導(dǎo)航鍵(NavigationBar)却邓,
     * true 表示使用的是手勢硕糊, 默認(rèn)是false
     */
    private static boolean navigationGestureEnabled(Context context) {
        int val = Settings.Global.getInt(context.getContentResolver(), getDeviceInfo(), 0);
        return val != 0;
    }
    /**
     * 獲取設(shè)備信息(目前支持幾大主流的全面屏手機(jī),親測華為腊徙、小米简十、oppo、魅族撬腾、vivo螟蝙、三星都可以)
     *
     * @return
     */
    private static String getDeviceInfo() {
        String brand = Build.BRAND;
        if(TextUtils.isEmpty(brand)) return "navigationbar_is_min";

        if (brand.equalsIgnoreCase("HUAWEI")||"HONOR".equals(brand)) {
            return "navigationbar_is_min";
        } else if (brand.equalsIgnoreCase("XIAOMI")) {
            return "force_fsg_nav_bar";
        } else if (brand.equalsIgnoreCase("VIVO")) {
            return "navigation_gesture_on";
        } else if (brand.equalsIgnoreCase("OPPO")) {
            return "navigation_gesture_on";
        } else if(brand.equalsIgnoreCase("samsung")){
            return "navigationbar_hide_bar_enabled";
        }else {
            return "navigationbar_is_min";
        }
    }

    /**
     * 判斷設(shè)備是否存在NavigationBar
     *
     * @return true 存在, false 不存在
     */
    public static boolean deviceHasNavigationBar() {
        boolean haveNav = false;
        try {
            //1.通過WindowManagerGlobal獲取windowManagerService
            // 反射方法:IWindowManager windowManagerService = WindowManagerGlobal.getWindowManagerService();
            Class<?> windowManagerGlobalClass = Class.forName("android.view.WindowManagerGlobal");
            Method getWmServiceMethod = windowManagerGlobalClass.getDeclaredMethod("getWindowManagerService");
            getWmServiceMethod.setAccessible(true);
            //getWindowManagerService是靜態(tài)方法,所以invoke null
            Object iWindowManager = getWmServiceMethod.invoke(null);

            //2.獲取windowMangerService的hasNavigationBar方法返回值
            // 反射方法:haveNav = windowManagerService.hasNavigationBar();
            Class<?> iWindowManagerClass = iWindowManager.getClass();
            Method hasNavBarMethod = iWindowManagerClass.getDeclaredMethod("hasNavigationBar");
            hasNavBarMethod.setAccessible(true);
            haveNav = (Boolean) hasNavBarMethod.invoke(iWindowManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return haveNav;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末时鸵,一起剝皮案震驚了整個(gè)濱河市胶逢,隨后出現(xiàn)的幾起案子厅瞎,更是在濱河造成了極大的恐慌,老刑警劉巖初坠,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件和簸,死亡現(xiàn)場離奇詭異,居然都是意外死亡碟刺,警方通過查閱死者的電腦和手機(jī)锁保,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來半沽,“玉大人爽柒,你說我怎么就攤上這事≌咛睿” “怎么了浩村?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵,是天一觀的道長占哟。 經(jīng)常有香客問我心墅,道長,這世上最難降的妖魔是什么榨乎? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任怎燥,我火速辦了婚禮,結(jié)果婚禮上蜜暑,老公的妹妹穿的比我還像新娘铐姚。我一直安慰自己,他們只是感情好肛捍,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布隐绵。 她就那樣靜靜地躺著,像睡著了一般篇梭。 火紅的嫁衣襯著肌膚如雪氢橙。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天恬偷,我揣著相機(jī)與錄音悍手,去河邊找鬼。 笑死袍患,一個(gè)胖子當(dāng)著我的面吹牛坦康,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播诡延,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼滞欠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了肆良?” 一聲冷哼從身側(cè)響起筛璧,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤逸绎,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后夭谤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體棺牧,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年朗儒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了颊乘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,912評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡醉锄,死狀恐怖乏悄,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情恳不,我是刑警寧澤檩小,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站妆够,受9級特大地震影響识啦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜神妹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望家妆。 院中可真熱鬧鸵荠,春花似錦、人聲如沸伤极。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽哨坪。三九已至庸疾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間当编,已是汗流浹背届慈。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留忿偷,地道東北人金顿。 一個(gè)月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像鲤桥,于是被迫代替她去往敵國和親揍拆。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容