How to check currently internet connection is available or not in android

前端 未结 18 1419
轮回少年
轮回少年 2020-12-04 15:40

I want to execute my application offline also, so I need to check if currently an internet connection is available or not. Can anybody tell me how to check if internet is av

18条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 15:59

    Use Below Code:

    private boolean isNetworkAvailable() {
      ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
      return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
    

    if isNetworkAvailable() returns true then internet connection available, otherwise internet connection not available

    Here need to add below uses-permission in your application Manifest file

    
    

提交回复
热议问题