How do I see if Wi-Fi is connected on Android?

前端 未结 22 2871
挽巷
挽巷 2020-11-22 05:56

I don\'t want my user to even try downloading something unless they have Wi-Fi connected. However, I can only seem to be able to tell if Wi-Fi is enabled, but they could sti

22条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 06:11

    Add this for JAVA:

    public boolean CheckWifiConnection() {
            ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
            if (conMgr.getActiveNetworkInfo() != null
                    && conMgr.getActiveNetworkInfo().isAvailable()
                    && conMgr.getActiveNetworkInfo().isConnected()) {
                return true;
            } else {
                return false;
            }
        }
    

    in Manifest file add the following permissions:

    
    

提交回复
热议问题