支持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
}
}