Android 安卓5+;如何更改首选网络

Android 安卓5+;如何更改首选网络,android,networking,reflection,root,jdk-internal-api,Android,Networking,Reflection,Root,Jdk Internal Api,您好,我已经读了很多关于这个论点的文章,但我认为在android 5+中如何改变首选网络存在很多错误。 首先,我的应用程序可以使用根目录,但作为一项必要条件,必须留在游戏商店 为此,我认为有三种方法: 带根 使用系统应用程序证书签署您的应用程序 使用运营商权限对应用程序进行签名 对于第二种方法,存在一些大问题: 您无法在play store中发布使用系统证书签名的应用程序 某些设备(如华为)上不能安装带有系统证书的应用程序 对于第三种方法,问题是沃达丰不是我的朋友:) 那么,让我们来谈谈第一

您好,我已经读了很多关于这个论点的文章,但我认为在android 5+中如何改变首选网络存在很多错误。
首先,我的应用程序可以使用根目录,但作为一项必要条件,必须留在游戏商店

为此,我认为有三种方法:

  • 带根
  • 使用系统应用程序证书签署您的应用程序
  • 使用运营商权限对应用程序进行签名
  • 对于第二种方法,存在一些大问题:

    • 您无法在play store中发布使用系统证书签名的应用程序
    • 某些设备(如华为)上不能安装带有系统证书的应用程序
    对于第三种方法,问题是沃达丰不是我的朋友:)

    那么,让我们来谈谈第一种方法:

    在play store中,有很多应用程序可以通过root权限更改首选网络,exmaple是auotate:
    我认为所有根会话中带有反射的应用程序调用都可以从内部API调用此函数:
    所以我试着用这个代码来做这件事。。。但冻结我的应用程序:

    /**
     * Function that change the preffered network
     * @param Context context - The context of application
     * @param String state_network - State that you want change ["2G" "3G" "4G" "2G/3G" "2G/3G/4G"]
     * @return boolean
     */
    public static String setDeviceNetwork(Context context, String state_network){
        try {
            Process process = null;
            process = Runtime.getRuntime().exec("su");
            process.waitFor();
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    
            try{
                Class<?> forName = Class.forName("com.android.internal.telephony.PhoneFactory");
                try {
                    Method getDefaultPhone = forName.getMethod("getDefaultPhone", new Class[]{});
                    getDefaultPhone.setAccessible(true);
                    try {
                        Object mPhone = getDefaultPhone.invoke(null, new Object[]{}); //@TODO qui!
                        Method setPreferredNetworkType = mPhone.getClass().getMethod("setPreferredNetworkType", new Class[]{int.class, Message.class});
                        Method getPreferredNetworkType = mPhone.getClass().getMethod("getPreferredNetworkType", new Class[]{Message.class});
                        setPreferredNetworkType.setAccessible(true);
                        setPreferredNetworkType.invoke(mPhone, new Object[]{1, "1"});
                        return "WORK!";
                    }catch (IllegalAccessException e){
                        return "Error IllegalAccessException on run command change network";
                    }catch (InvocationTargetException e){
                        return "Error InvocationTargetException on run command change network";
                    }
                }catch (NoSuchMethodException e){
                    return "Error NoSuchMethodException on find method";
                }
            }catch (ClassNotFoundException e){
                return "Error ClassNotFoundException on find class";
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "Error IOException on sudo command";
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            return "Error InterruptException on sudo command";
        }
    }
    
    /**
    *用于更改首选网络的函数
    *@param Context-应用程序的上下文
    *@param String state_network-您要更改的状态[“2G”“3G”“4G”“2G/3G”“2G/3G/4G”]
    *@返回布尔值
    */
    公共静态字符串setDeviceNetwork(上下文上下文、字符串状态\u网络){
    试一试{
    Process=null;
    process=Runtime.getRuntime().exec(“su”);
    process.waitFor();
    TelephonyManager TelephonyManager=(TelephonyManager)context.getSystemService(context.TELEPHONY_服务);
    试一试{
    Class-forName=Class.forName(“com.android.internal.telephony.PhoneFactory”);
    试一试{
    方法getDefaultPhone=forName.getMethod(“getDefaultPhone”,新类[]{});
    getDefaultPhone.setAccessible(true);
    试一试{
    Object mPhone=getDefaultPhone.invoke(null,新对象[]{});//@TODO qui!
    方法setPreferredNetworkType=mPhone.getClass().getMethod(“setPreferredNetworkType”,新类[]{int.Class,Message.Class});
    方法getPreferredNetworkType=mPhone.getClass().getMethod(“getPreferredNetworkType”,新类[]{Message.Class});
    setPreferredNetworkType.setAccessible(true);
    调用(mPhone,新对象[]{1,“1”});
    返回“工作!”;
    }捕获(非法访问例外e){
    返回“运行命令更改网络时出现错误IllegalAccessException”;
    }捕获(调用TargetException e){
    返回“运行命令更改网络上的错误调用TargetException”;
    }
    }捕获(无此方法例外){
    返回“查找方法时出现错误NoSuchMethodException”;
    }
    }catch(classnotfounde异常){
    返回“查找类时出错ClassNotFoundException”;
    }
    }捕获(IOE异常){
    e、 printStackTrace();
    返回“错误IOException on sudo命令”;
    }
    捕捉(中断异常e){
    e、 printStackTrace();
    返回“错误中断sudo命令异常”;
    }
    }
    

    关于这个问题有什么解决方案吗?

    我认为它冻结了,因为process.waitFor();不要回来,你把这个用起来了吗?