r1si
r1si

Reputation: 1164

Android 5+ how to change preferred network

I have read a lot about this argument, but I think there is a lot of mistake to how can change preferred network in android 5+.

First of all my app can use root, but as a requisite it must be stay in play store.

To do this I think there is three methods:

  1. with root
  2. sign your app with system app cert
  3. sign your app with a carrier permission

For the second method there is some big problems:

For the third method, the problem is that Vodafone is not my friends :)

So, let's talking about the first method:

Inside play store there are a lot of app that can change preferred network with root permission, an example is Automate: https://play.google.com/store/apps/details?id=com.llamalab.automate.ext.superuser&hl=it

I think all application call with reflection in root session this function from internal API : https://github.com/android/platform_frameworks_base/blob/nougat-mr1-release/telephony/java/com/android/internal/telephony/ITelephony.aidl#L753

So I have try to do this with this code... but it freezes my application:

/**
 * Function that change the preferred 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";
    }
}

Any solution for this problem?

Upvotes: 2

Views: 327

Answers (0)

Related Questions