How to check if mobile network is on / off

I would like to know if the mobile network is on or off.

My application is designed to help the user when he receives a phone call, and for this I need Internet access. Thus, I would like to display an information window when the user first accesses the application if Wi-Fi has a sleep policy and the mobile network is disconnected. (I need internet for milliseconds after the phone rings).

I found Settings.System.WIFI_SLEEP_POLICY, but I can not find any information on how to check if the mobile network is disconnected (when Wi-Fi is turned on and working).

Any help would be appreciated!

Edit: The problem is that I want to know if the mobile network is turned on by the user (while the phone could have access to WiFi at that time).

+5
source share
5 answers

I finally found a solution. Apparently, not all phones have this option:

Home> Menu> Settings> Wireless & networks> Mobile network (check box)

However, for those who do this, this method will work:

/**
 * @return null if unconfirmed
 */
public Boolean isMobileDataEnabled(){
    Object connectivityService = getSystemService(CONNECTIVITY_SERVICE); 
    ConnectivityManager cm = (ConnectivityManager) connectivityService;

    try {
        Class<?> c = Class.forName(cm.getClass().getName());
        Method m = c.getDeclaredMethod("getMobileDataEnabled");
        m.setAccessible(true);
        return (Boolean)m.invoke(cm);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
+18
source

there seems to be an alternative, cleaner solution, and then the Reflection approach:

final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
int type = networkInfo.getType();
String typeName = networkInfo.getTypeName();
boolean connected = networkInfo.isConnected()

networkInfo.getType () will return '0' when connected to a mobile network or "1" when connected via WIFI. networkInfo.getTypeName () will return the string "mobile" or "WIFI". and networkInfo.isConnected () will tell you if you have an active connection.

+3
source

ANDROID 5.0+ (API 21 +)

getMobileDataEnabled NoSuchMethodException Android 5.0+ . , , NoSuchMethodException.

...
catch(NoSuchMethodException e)
{
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
  {
      isDataEnabled = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0) == 1;
  }
}
+3
    PackageManager pm = context.getPackageManager();
    boolean hasTelephony = pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
0

, API:

ConnectivityManager cm =
            (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null &&
                          activeNetwork.isConnectedOrConnecting();

if(isConnected)
{
if(activeNetwork.getType()==ConnectivityManager.TYPE_MOBILE)
return true;    

else
    return false;
}

else
    return false;
0

All Articles