I want to get the "Use mobile networks" settings value (true or false). There's a way of knowing if wifi is enabled and it can be done like this:
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int state = wm.getWifiState();
boolean pref = state == WifiManager.WIFI_STATE_ENABLED || state == WifiManager.WIFI_STATE_ENABLING;
But how can I do the same check for data?
i hope it helps :)
ConnectivityManager connect = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Boolean isConnected = connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
You can try this:
public boolean getMobileDataEnabled() throws Exception {
ConnectivityManager mcm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Class ownerClass = mcm.getClass();
Method method = ownerClass.getMethod("getMobileDataEnabled");
return (Boolean)method.invoke(mcm);
}
and add the permission to AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With