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

前端 未结 22 2902
挽巷
挽巷 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:16

    While Jason's answer is correct, nowadays getNetWorkInfo (int) is a deprecated method. So, the next function would be a nice alternative:

    public static boolean isWifiAvailable (Context context)
    {
        boolean br = false;
        ConnectivityManager cm = null;
        NetworkInfo ni = null;
    
        cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        ni = cm.getActiveNetworkInfo();
        br = ((null != ni) && (ni.isConnected()) && (ni.getType() == ConnectivityManager.TYPE_WIFI));
    
        return br;
    }
    

提交回复
热议问题