I am developing one Android app for that I need to enable/disable mobile internet problematically.
The code I have written for do this works fine with all other companies devices except SONY Xperia Z.
Is there any other way to stop the mobile internet in SONY?
My code to enable/disable mobile data:
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);
To manage internet on Android device I use this code maybe it could help you:
ConnectivityManager dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = dataManager.getActiveNetworkInfo();
Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataMtd.setAccessible(true);
dataMtd.invoke(dataManager, true); // set the correct boolean value to enable or disable internet
I have created a sample application to manage wifi and internet with Android if you need more details