Automatically turn on / off Mobile data connection

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

Hi. Just to better understand; can you give an example when this is needed?
Further I don’t see that you gave the MODIFY_PHONE_STATE permission.
Another thing with this “workaround” is that it will only work on rooted phones.

What you can do legally is open your network settings menu programmatically, like:

import android.content.Intent;

void setup() {
  fullScreen();
  orientation (PORTRAIT);
  background (0, 0, 150);
  textSize (60);
  textAlign (CENTER);
  text ("Click to open network settings menu.", width/2, height/2);
}

void draw () {
}

void mousePressed () {
  Intent intent = new Intent(Intent.ACTION_MAIN);
  intent.setClassName("com.android.phone", "com.android.phone.NetworkSetting");
  startActivity(intent);
}

Hi Noel,

Thanks You

I am making an application that allows detecting the texts copied on the network, however it can barely work 1 minute before the ip is blocked and it does not allow any more queries, so I want to automatically connect and disconnect to get a new IP.

Would that code work with root? Does the exception jump for this?
If you confirm it, I will try to rotate an old phone to see if it works

A greatings and thanks so much

Do you mean something like Pigeon app, that copies text to the clipboard and then transmit to other devices? That can be done with oscP5.
Who detects and blocks the IP? Can’t you use VPN?

Not, something like: https://www.copyscape.com/
I need 20-30 querry for check a page
Google, Bing … allow a few queries before blocking with catpcha for 1 hour. I am not sure that VPN can solved it.