判斷是否隱藏虛擬導航鍵、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);
        });

判斷是否隱藏虛擬導航鍵---嘗試方法

    /**
     *
     * @param context
     * @return 返回true表示顯示虛擬導航鍵、false表示隱藏虛擬導航鍵
     */
    public static boolean hasNavigationBar(Context context) {
        //navigationGestureEnabled()從設置中取不到值的話,返回false,因此也不會影響在其他手機上的判斷
        return deviceHasNavigationBar() && !navigationGestureEnabled(context);
    }
    /**
     * 獲取主流手機設置中的"navigation_gesture_on"值,判斷當前系統(tǒng)是使用導航鍵還是手勢導航操作
     * @param context app Context
     * @return
     * false 表示使用的是虛擬導航鍵(NavigationBar),
     * true 表示使用的是手勢, 默認是false
     */
    private static boolean navigationGestureEnabled(Context context) {
        int val = Settings.Global.getInt(context.getContentResolver(), getDeviceInfo(), 0);
        return val != 0;
    }
    /**
     * 獲取設備信息(目前支持幾大主流的全面屏手機,親測華為、小米、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";
        }
    }

    /**
     * 判斷設備是否存在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;
    }
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容