Hello,
I am trying to make an application that allows activating and deactivating the mobile connection to the Internet in order to change IP
I have found code but it doesn’t work right.
This line gives an error: setMobileDataEnabledMethod.invoke(telephonyService, true);//MISTAKE Exception
Does anyone know how I could fix it?
void setup() {
  fullScreen();
  noStroke();
  fill(0); text("...", width/10, height/10); setMobileDataState(false);delay(3000);
   try {
          Activity act = this.getActivity();
  
  Context context = act.getApplicationContext();
   
         TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
         Method setMobileDataEnabledMethod = Objects.requireNonNull(telephonyService).getClass().getDeclaredMethod("setDataEnabled", boolean.class);
         setMobileDataEnabledMethod.invoke(telephonyService, true);
      } catch (Exception ex) {
        text("Exception",222,22); /*Log.e("MainActivity", "Error setting mobile data state", ex);*/
      }delay(8000);
}
void draw() {
  background(204);
  
    if (isOnline()) {
      setMobileDataState(false);
      rect(0, 0, width/2, height);delay(5000);
     fill(150,1,1);  text("OK-1",22,22); fill(0); 
       try {
          Activity act = this.getActivity();
  
  Context context = act.getApplicationContext();
   
         TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
         Method setMobileDataEnabledMethod = Objects.requireNonNull(telephonyService).getClass().getDeclaredMethod("setDataEnabled", boolean.class);
         setMobileDataEnabledMethod.invoke(telephonyService, false);
      } catch (Exception ex) {
        fill(150,1,1);   text("Exception",222,22);fill(0);  /*Log.e("MainActivity", "Error setting mobile data state", ex);*/
      }
      
      
      // Left
    } else {
   fill(0);    rect(width/2, 0, width/2, height); // Right
    fill(150,1,1); text("1",22,82);  setMobileDataState(true);  text("OK-2",22,22);   try {
          text("2",38,82); Activity act = this.getActivity();
  text("3",52,82); 
  Context context = act.getApplicationContext();
   text("4",72,82); 
         TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
         text("5",92,82); 
         Method setMobileDataEnabledMethod = Objects.requireNonNull(telephonyService).getClass().getDeclaredMethod("setDataEnabled", boolean.class);
         text("6",122,82); 
         setMobileDataEnabledMethod.invoke(telephonyService, true);//MISTAKE Exception 
         
        
          text("7",152,82); 
      } catch (Exception ex) {
        fill(150,1,1);   text("ERROR",222,22);fill(0);  /*Log.e("MainActivity", "Error setting mobile data state", ex);*/
      }delay(5000);
      fill(0); 
    }
  
}
// Download and use a SQLite DB on Android & Processing
// 28-02-2016 - By Rasmus C.
// Database
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
// isOnline()
import android.net.NetworkInfo;
import android.net.ConnectivityManager;
// Misc
import android.os.Environment;
import android.content.Context;
import android.app.Activity;
 import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
//import androidx.appcompat.app.AppCompatActivity;
import java.lang.reflect.Method;
import java.util.Objects;
//import static org.junit.Assert.*;
//import androidx.appcompat.app.AppCompatActivity;
// Paths - Used in setup
String DBDir; // DB dir (without filename)
File DBFile; // DB dir (with filename)
 
// Objects - Used in food_desc()
SQLiteDatabase database = null; // DB object
Cursor c = null; // Cursor object
 
 import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
Boolean isOnline(){
  Activity act = this.getActivity();
  
  Context context = act.getApplicationContext();
   
  ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
  NetworkInfo ni = cm.getActiveNetworkInfo();
  if(ni != null && ni.isConnected()){
    return true;
  }
  return false;
}
  
void setMobileDataState(boolean mobileDataEnabled) {
      try {
          Activity act = this.getActivity();
  
  Context context = act.getApplicationContext();
   
         TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
         Method setMobileDataEnabledMethod = Objects.requireNonNull(telephonyService).getClass().getDeclaredMethod("setDataEnabled", boolean.class);
         setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
      } catch (Exception ex) {
         /*Log.e("MainActivity", "Error setting mobile data state", ex);*/
      }
   }
   
 /*  public boolean getMobileDataState() {
      try {
         TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
         Method getMobileDataEnabledMethod = Objects.requireNonNull(telephonyService).getClass().getDeclaredMethod("getDataEnabled");
         return (boolean) (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
      } catch (Exception ex) {
         Log.e("MainActivity", "Error getting mobile data state", ex);
      }
      return false;
   }*/
   
 
 /*//Other code problems witch Field class  private void setMobileDataEnabled(Context context, boolean enabled) {
    final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);
    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;*/
A greatings and thanks,
Anthony20