I want to check whether mobile data is on/off, and for this I use the below code:
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true); // Make the method callable
// get the setting for "mobile data"
mobileDataEnabled = (Boolean) method.invoke(cm);
However, this code does not work on some devices, and throws an exception that no such method exists
.
I have found it is not working in Android 4.1.x. Why is it throwing this execption, and how can I fix it?