How to check if data transfer is enabled on the device? Just need to clarify that I was not interested in the presence of the Internet, I need an answer: on / off
2 answers
How to check if data transfer is enabled on the device?
For example :
boolean mobileYN = false; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { mobileYN = Settings.Global.getInt(getApplicationContext().getContentResolver(), "mobile_data", 1) == 1; } else { mobileYN = Settings.Secure.getInt(getApplicationContext().getContentResolver(), "mobile_data", 1) == 1; } - does not work correctly - Jorik
- @Jorik, On which version of android? - post_zeew
- 4.4.2. Like the 19 API. - Jorik
- @Jorik, Just checked on 4.4.2 - it works. - post_zeew
- I have included GPS and WiFi, but mob. Internet Off - True True - Jorik
|
There is also a correct answer:
boolean mobileDataEnabled = false; // Assume disabled ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); try { 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); } catch (Exception e) { // Some problem accessible private API // TODO do whatever error handling you want here } |