How to check if mobile network is enabled/disabled

前端 未结 5 1887
别跟我提以往
别跟我提以往 2020-12-09 23:31

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 I

相关标签:
5条回答
  • 2020-12-09 23:48
        PackageManager pm = context.getPackageManager();
        boolean hasTelephony = pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
    
    0 讨论(0)
  • 2020-12-10 00:01

    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;
      }
    }
    
    0 讨论(0)
  • 2020-12-10 00:02

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-10 00:03

    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;
    
    0 讨论(0)
  • 2020-12-10 00:04

    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.

    0 讨论(0)
提交回复
热议问题