简体   繁体   English

检查Internet连接仅返回false的布尔方法

[英]Boolean Method for checking Internet Connectivity returning only false

In my application I am checking whether the mobile is connected to the internet or not, using WIFI or mobile data connection but the problem is my code is always showing internet connection not available through my device is connected to WIFI. 在我的应用程序中,我正在使用WIFI或移动数据连接检查移动设备是否已连接到Internet,但问题是我的代码始终显示无法通过设备连接到WIFI的互联网连接。

Here is the code where I am inspecting the internet connection. 这是我检查互联网连接的代码。

public class InternetDetector {
     private Context mcontext;

public InternetDetector(Context context) {
    this.mcontext = context;
}


    public boolean checkMobileInternetConn() {
        ConnectivityManager connectivity = (ConnectivityManager) mcontext
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivity != null) {
            NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if (info != null) {
                if (info.isConnected()) {
                    return true;
                }
            }
        }

        else if (connectivity != null) {
            NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (info != null) {
                if (info.isConnected()) {
                    return true;
                }
            }
        }


        return false;
    }


}

and this is how I am using this class in other activities to check the connection.But the code is always going to the else part and always shows the toast. 这就是我在其他活动中使用此类检查连接的方式。但是代码始终转到else部分,并始终显示吐司。

if (allValid) {

         isConnectionExist = internetDetector.checkMobileInternetConn();
           if (isConnectionExist) {

              try {
                   new MyAsyncClass().execute();

                   } catch (Exception ex) {
                     Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_SHORT).show();
                            }

                        } else {
                            Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
                        }


                    }

Permissions which I have given under Manifest file are:- 我在清单文件下给出的权限是:-

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

Alternate solution: 替代解决方案:

public boolean hasConnectivity() {
      ConnectivityManager connectivityManager = (ConnectivityManager) 
      context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
      return activeNetwork != null;
}

I replaced isConnectionExist variable with hasConnectivity() 我用hasConnectivity()替换了isConnectionExist变量

if (allValid) {


       if (hasConnectivity()) {

          try {
               new MyAsyncClass().execute();

               } catch (Exception ex) {
                 Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_SHORT).show();
                        }

                    } else {
                        Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
                    }


                }
fun isOnline(context: Context): Boolean {
        try {
            val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val nInfo = cm.activeNetworkInfo
            return nInfo != null && nInfo.isConnected
        } catch (e: Exception) {
            e.printStackTrace()
            return false
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM