Android Question Change Network operator with code

walmo

Member
Licensed User
Hi
Please There must be a way to change the network operator on a rooted device.
I found this code that use the TelephonyManager to change data state and it is working
very nice , but i also need to change the network operator.

B4X:
Sub Process Globals
    Dim joData As JavaObject
End Sub

Sub ToggleData(dataState as Boolean)
    If joData.IsInitialized = False Then
        joData.InitializeContext
    End If
    joData.RunMethod("changeConnection", Array(dataState))      
End Sub

#If JAVA
import android.content.Context;
import android.os.Build;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.net.ConnectivityManager;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

    Context context1;
    public void changeConnection(boolean enable) {
        context1 = this.getApplicationContext();
        try{
            StringBuilder command = new StringBuilder();
            command.append("su -c ");
            command.append("service call phone ");
            command.append(getTransactionCode() + " ");
            if (Build.VERSION.SDK_INT >= 22) {
                SubscriptionManager manager = SubscriptionManager.from(context1);
                int id = 0;
                if (manager.getActiveSubscriptionInfoCount() > 0)
                    id = manager.getActiveSubscriptionInfoList().get(0).getSubscriptionId();
                command.append("i32 ");
                command.append(String.valueOf(id) + " ");
            }
            command.append("i32 ");
            command.append(enable?"1":"0");
            command.append("\n");
            Runtime.getRuntime().exec(command.toString());
        }catch(IOException e){
     //       ...
        }
    }
    private String getTransactionCode() {
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context1.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
            Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
            getITelephonyMethod.setAccessible(true);
            Object ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
            Class<?> ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
  
            Class<?> stub = ITelephonyClass.getDeclaringClass();
            Field field = stub.getDeclaredField("TRANSACTION_setDataEnabled");
            field.setAccessible(true);
            return String.valueOf(field.getInt(null));
        } catch (Exception e) {
            if (Build.VERSION.SDK_INT >= 22)
                return "86";
            else if (Build.VERSION.SDK_INT == 21)
                return "83";
        }
        return "";
    }  
#End If

Thanks
 
Top