How to check Wifi is connected, but no Internet access in Android

后端 未结 4 1946
挽巷
挽巷 2020-12-28 19:35

I would like to know why wifi is connected but there is no internet access in Android. How can i check it? My code is:

ConnectivityManager cn=(ConnectivityMa         


        
4条回答
  •  悲&欢浪女
    2020-12-28 20:00

    Use this :

    public static boolean isInternetOn(Context context) {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            // test for connection
            if (cm.getActiveNetworkInfo() != null
                    && cm.getActiveNetworkInfo().isAvailable()
                    && cm.getActiveNetworkInfo().isConnected()) {
                Log.v(TAG, "Internet is working");
                // txt_status.setText("Internet is working");
                return true;
            } else {
                // txt_status.setText("Internet Connection Not Present");
                Log.v(TAG, "Internet Connection Not Present");
                return false;
            }
        }
    

    Hope this helps.

提交回复
热议问题