how to check if wifi is really connected in Android

前端 未结 4 951
一生所求
一生所求 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:14

    getNetworkInfo(int) method is deprecated. You can apply something like this

     ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
        if (activeNetwork != null) { 
            // connected to the internet
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                // connected to wifi
    
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                // connected to the mobile network
            }
        } else {
            // not connected to the internet
        }
    

    Also, please add this permission under AndroidManifest.xml

    
    

提交回复
热议问题