How to check the internet connectivity within the network in Android (using internet of some other device through HOTSPOT)

馋奶兔 提交于 2019-12-30 01:03:46

问题


I have a requirement where I want to check whether there is any internet connectivity when I am connected with the network.

For example, I have device A and device B.

Device A is connected with hotspot with that of device B. In device A, I get it as connected with Wi-Fi and in device B - one device connected with hotspot.

Now, if I remove the internet from device B (not the tethering hotspot), then in device A, it still shows - connected with Wi-Fi but there is no internet connectivity.

Classes like ConnectivityManager help in determining whether a device is connected with the network not about the internet connectivity.

I want to track this issue. Is there any way to achieve this?


回答1:


It can be a ridiculous solution but i think also it could be real solution:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000);
            urlc.connect();
            if (urlc.getResponseCode() == HttpURLConnection.HTTP_OK) {
                return new Boolean(true);
            }
        } catch (MalformedURLException mue) {
            // TODO Auto-generated catch block
            mue.printStackTrace();
        } catch (IOException ie) {
            // TODO Auto-generated catch block
            ie.printStackTrace();
        }
    }
    return false;
}



回答2:


Try the below function to check your internet connection:

    public static boolean isInternetConnected(Context mContext) {

        try {
            ConnectivityManager connect = null;
            connect = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            if (connect != null) {
                NetworkInfo resultMobile = connect
                        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

                NetworkInfo resultWifi = connect
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

                if ((resultMobile != null && resultMobile
                        .isConnectedOrConnecting())
                        || (resultWifi != null && resultWifi
                                .isConnectedOrConnecting())) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

Add the following permissions to your manifest file,

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


来源:https://stackoverflow.com/questions/26056015/how-to-check-the-internet-connectivity-within-the-network-in-android-using-inte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!