Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has the user enabled the mobile data?

Tags:

android

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?

like image 544
Iiro Krankka Avatar asked Jul 20 '26 03:07

Iiro Krankka


2 Answers

i hope it helps :)

ConnectivityManager connect = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

Boolean isConnected = connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
like image 195
Mert Avatar answered Jul 28 '26 18:07

Mert


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"/>
like image 28
PageNotFound Avatar answered Jul 28 '26 17:07

PageNotFound



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!