Android Question Turn on/off data connection

devmobile

Active Member
Licensed User
I saw below code in stackoverflow
B4X:
private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
    connectivityManagerField.setAccessible(true);
    final Object connectivityManager = connectivityManagerField.get(conman);
    final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}
How do i use it with java inline?
 

Alexander Stolte

Expert
Licensed User
Longtime User
B4X:
#If JAVA

private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
 final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 final Class conmanClass = Class.forName(conman.getClass().getName());
 final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
 connectivityManagerField.setAccessible(true);
 final Object connectivityManager = connectivityManagerField.get(conman);
 final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
 final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
 setMobileDataEnabledMethod.setAccessible(true);

 setMobileDataEnabledMethod.invoke(connectivityManager, enabled);}

#End If

And the rest is in the Tutorial here. It is easy :)
 
Upvote 0

devmobile

Active Member
Licensed User
B4X:
#If JAVA

private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
connectivityManagerField.setAccessible(true);
final Object connectivityManager = connectivityManagerField.get(conman);
final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);

setMobileDataEnabledMethod.invoke(connectivityManager, enabled);}

#End If

And the rest is in the Tutorial here. It is easy :)
No tired
i use it and get error
src\b4a\example\main.java:408: error: cannot find symbol
public void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
^
symbol: class Context
location: class main
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I found it from stackoverflow
The code further down in the same stactoverflow thread might be easier to use.

You will need the JavaObject library to be enabled
You will also have to import some Android classes after the #If Java line such as ConnectivityManager. If you put the Java code in the Main activity of your B4A project then you don't need to import Android class Context.
 
Upvote 0
Top