5

I would like to know if the mobile network is enabled or disabled.

My application is designed to help the user when he receives a phone call, and to do this I need Internet access. Thus, I would like to display an information box when the user access the application for the first time if Wi-Fi has a sleep policy and Mobile network is disabled. (I need Internet within milliseconds after the phone start ringing).

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

Any help would be appreciated!

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

Cremons
  • 990
  • 1
  • 9
  • 11
  • 1
    exact duplicate of http://stackoverflow.com/questions/8211081/android-to-check-if-the-phone-has-a-network-provider/8211179#8211179 – Jan Dragsbaek Nov 22 '11 at 08:51
  • 1
    It's not the same. ((LocationManager)getSystemService(Context.LOCATION_SERVICE)).isProviderEnabled(LocationManager.NETWORK_PROVIDER);is true when mobile network is disabled and Wi-Fi is enabled (and working). From API: location based on availability of cell tower and WiFi access points – Cremons Nov 22 '11 at 09:59

5 Answers5

19

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

Home > Menu > Settings > Wireless & networks > Mobile network (checkbox)

However, for those who do, 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;
    }
}
Cremons
  • 990
  • 1
  • 9
  • 11
  • 2
    Don't forget to add in your AndroidManifest.xml – Bao Le May 11 '12 at 10:34
  • @DanielNugent any ideas why? – Peter Chaula Nov 22 '16 at 18:33
  • @PeterChaula This was a security hole, it was unintentional that this was even possible in the first place. Google fixed the security hole, so it's no longer possible to do this. See here for more info: http://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later – Daniel Nugent Nov 22 '16 at 18:35
  • @DanielNugent one reason why one must always avoid "hacky" reflection as much as possible. – Peter Chaula Nov 22 '16 at 18:37
3

apparently there is an alternative, more clean solution 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 mobile network or '1' when connected trough WIFI. networkInfo.getTypeName() will return the strings "mobile" or "WIFI". and networkInfo.isConnected() will tell you whether or not you have an active connection.

bvanvelsen - ACA Group
  • 1,741
  • 1
  • 14
  • 25
3

UPDATE FOR ANDROID 5.0+ (API 21+)

Calling getMobileDataEnabled via the reflection leads to NoSuchMethodException on Android 5.0+ on some devices. So in addition to accepted answer you may wanna do second check if NoSuchMethodException is thrown.

...
catch(NoSuchMethodException e)
{
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
  {
      isDataEnabled = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0) == 1;
  }
}
stevo.mit
  • 4,681
  • 4
  • 37
  • 47
1

you can use below code this is working for all API versions:

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;
Arun kumar
  • 1,894
  • 3
  • 22
  • 28
  • This will tell me about my active network. I want to know if mobile network is turned of by the user. If I'm connected to wifi the answer could still be both yes and no. – Cremons May 04 '15 at 13:32
  • this only get active network, if user enable both wifi and 3b, then always return TYPE_WIFI if connected by wifi – kemdo Apr 05 '18 at 02:58
0
    PackageManager pm = context.getPackageManager();
    boolean hasTelephony = pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);