我偶然发现了一种通过反射和系统调用命令来解决这个问题的方法,并决定报告它,即使线程很旧并且有一些警告:
- 需要根
- Hackish 并且可能特定于 ROM(在 CM 12.1 titan 上测试)
- 可能不适用于所有 android 版本(在 5.1.1 上测试)
大部分代码是从this answer by ChuongPham 借用/启发的。
首先,我们需要通过获取 ITelephony 类的声明字段的值来获取正确的事务代码。由于我怀疑字段名称可能会因平台而略有不同(我的字段名称是“TRANSACTION_setPreferredNetworkType_96”),因此我提供了一个尽可能灵活的解决方案:
private static String get3gTransactionCode(Context context) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
final TelephonyManager mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final Class<?> mTelephonyClass = Class.forName(mTelephonyManager.getClass().getName());
final Method mTelephonyMethod = mTelephonyClass.getDeclaredMethod("getITelephony");
mTelephonyMethod.setAccessible(true);
final Object mTelephonyStub = mTelephonyMethod.invoke(mTelephonyManager);
final Class<?> mTelephonyStubClass = Class.forName(mTelephonyStub.getClass().getName());
final Class<?> mClass = mTelephonyStubClass.getDeclaringClass();
for (Field f:mClass.getDeclaredFields()) {
if (f.getName().contains("setPreferredNetworkType")) {
final Field field = mClass.getDeclaredField(f.getName());
field.setAccessible(true);
return String.valueOf(field.getInt(null));
}
}
throw new NoSuchFieldException();
}
接下来我们可以通过 su 在系统调用中使用事务代码:
private static void setPreferredNetworkType(Context context, int preferredType) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {
String transactionCode = get3gTransactionCode(context);
String command = "service call phone " + transactionCode + " i32 " + preferredType;
executeCommandViaSu(context, "-c", command);
}
在我的例子中,我调用该方法,第二个参数是 1 代表 2G,10 代表 3G 偏好。不同网络类型的常量可以在here找到。
为了方便和完整,我还在此处从ChuongPham's answer 复制粘贴 executeCommandViaSu 方法:
private static void executeCommandViaSu(Context context, String option, String command) {
boolean success = false;
String su = "su";
for (int i=0; i < 3; i++) {
// Default "su" command executed successfully, then quit.
if (success) {
break;
}
// Else, execute other "su" commands.
if (i == 1) {
su = "/system/xbin/su";
} else if (i == 2) {
su = "/system/bin/su";
}
try {
// Execute command as "su".
Runtime.getRuntime().exec(new String[]{su, option, command});
} catch (IOException e) {
success = false;
// Oops! Cannot execute `su` for some reason.
// Log error here.
} finally {
success = true;
}
}
}