I search a looooooot but I didn't find what I want.
I'm trying to make an app that the user are able to turn on and turn off by pressing a button for each one.
I found some codes using the reflection (I'll use the android 2.3 and up) but I don't know how to adapt it to use with buttons.
I search how reflection works, I spend a lot of time looking for other ways to do it but... anyone works for me.
The worst problem is the codes and examples I found didn't use the exceptions (throws or try catch) so I don't know how to work with them.
This is the code I found here, but I don't know how to use it (or call it as true of false, in order to Enable and Disable the 3G/Mobile data) in a Button click.
Anyone can help me?
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);
}
Link to where I found this code: How can i turn off 3G/Data programmatically on Android?