how to check if wifi is really connected in Android

前端 未结 4 1071
一生所求
一生所求 2021-01-15 12:04

I\'d like my android device to connect to a wifi hotspot. I created a new wificonfiguration and add it into the wifimanager, this wificonfigu

4条回答
  •  醉酒成梦
    2021-01-15 12:20

    This may help you .

    public static boolean isInternetAvailable(Context context) {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;
        boolean connectionavailable = false;
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        NetworkInfo informationabtnet = cm.getActiveNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            try {
                if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                    if (ni.isConnected()) haveConnectedWifi = true;
                if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                    if (ni.isConnected()) haveConnectedMobile = true;
                if (informationabtnet.isAvailable()
                    && informationabtnet.isConnected())
                    connectionavailable = true;
                Log.i("ConnectionAvailable", "" + connectionavailable);
            } catch (Exception e) {
                System.out.println("Inside utils catch clause , exception is"
                    + e.toString());
                e.printStackTrace();
            }
        }
        return haveConnectedWifi || haveConnectedMobile;
    }
    

提交回复
热议问题