我偶然发现了一种用反射和系统调用命令来解决这个问题的方法,并决定报告它,即使线程已经过时并且有一些警告:
>需要根
> Hackish并且可能特定于ROM(在CM 12.1 titan上测试)
>可能不适用于所有Android版本(在5.1.1上测试)
首先,我们需要通过获取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,NoSuchFieldException,InvocationTargetException {
String transactionCode = get3gTransactionCode(context);
String command = "service call phone " + transactionCode + " i32 " + preferredType;
executeCommandViaSu(context,"-c",command);
}
在我的情况下,我将该方法称为2G的第二个参数为1,3G首选项为10.不同网络类型的常量可以在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;
}
}
}