android 代码怎么切换2 3g,android – 如何从3G和2G / EDGE切换反之亦然?

我偶然发现了一种用反射和系统调用命令来解决这个问题的方法,并决定报告它,即使线程已经过时并且有一些警告:

>需要根

> 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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值