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

时间:2017-12-26 11:41:57

标签: java android android-connectivitymanager

在我的应用程序中,我正在检查移动设备是否已连接到互联网,使用WIFI或移动数据连接,但问题是我的代码始终显示无法通过我的设备连接到WIFI的互联网连接。

以下是我正在检查互联网连接的代码。

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;
    }


}

这就是我在其他活动中使用这个类来检查连接的方法。但是代码总是转到else部分并且总是显示toast。

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();
                        }


                    }

我在Manifest文件下提供的权限是: -

<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"/>

3 个答案:

答案 0 :(得分:0)

替代解决方案:

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

答案 1 :(得分:0)

我用 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();
                    }


                }

答案 2 :(得分:0)

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
        }

    }