android13实现切换导航模式功能

支持android13以上系统,需要系统签名。

public class NavigationHelper {


    /**
     * 设置导航模式
     *
     * @param context
     * @param mode    GESTURAL:手势 TWOBUTTON:二按钮 THREEBUTTON:三按钮
     */
    public static void setNavigationMode(Context context, Mode mode) {
        try {
            Object mIOverlayManager = context.getSystemService("overlay");
            Method setEnabledExclusiveInCategory = mIOverlayManager.getClass().getMethod("setEnabledExclusiveInCategory", String.class, UserHandle.class);
            setEnabledExclusiveInCategory.setAccessible(true);
            String overlayPackage = "com.android.internal.systemui.navbar.gestural";
            if (mode == Mode.TWOBUTTON) {
                overlayPackage = "com.android.internal.systemui.navbar.twobutton";
            } else if (mode == Mode.THREEBUTTON) {
                overlayPackage = "com.android.internal.systemui.navbar.threebutton";
            }
            UserHandle userHandle = (UserHandle) newInstance(UserHandle.class, new Class[]{int.class}, 0);
            setEnabledExclusiveInCategory.invoke(mIOverlayManager, overlayPackage, userHandle);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取导航模式
     * @param context
     * @return
     */
    public static Mode getNavigationMode(Context context) {
        try {
            ContentResolver resolver = context.getApplicationContext().getContentResolver();
            int mode = Settings.Secure.getInt(resolver, "navigation_mode");
            if (mode == 0) {
                return Mode.THREEBUTTON;
            } else if (mode == 2) {
                return Mode.GESTURAL;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 开启经典导航模式
     *
     * @param enable
     */
    public static void setClassicNavigationMode(boolean enable) {
        try {
            Class c = Class.forName("android.os.SystemProperties");
            Method method = c.getMethod("set", String.class, String.class);
            method.setAccessible(true);
            method.invoke(null, "persist.sys.classic.navigation.mode", String.valueOf(enable));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private static Object newInstance(Class clazz, Class[] argsType, Object... args) {
        Object instance = null;
        try {
            Constructor constructor = clazz.getConstructor(argsType);
            constructor.setAccessible(true);
            instance = constructor.newInstance(args);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return instance;
    }

    public enum Mode {
        GESTURAL,
        TWOBUTTON,
        THREEBUTTON
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值