How to check if mobile network is enabled/disabled

前端 未结 5 1947
别跟我提以往
别跟我提以往 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-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;
      }
    }
    

提交回复
热议问题