简体   繁体   English

如何确定设备是否正在连接或连接到Wi-Fi热点?

[英]How to determine wether the device is connecting or connected to a Wi-Fi hotspot?

First I use the method WifiManager.getWifiState() to determine wether my device's Wi-Fi is enabled or not. 首先,我使用方法WifiManager.getWifiState()确定是否启用了设备的Wi-Fi。 If it's enabled, WifiManager.getConnectionInfo() returns a WifiInfo object with SSID and other informations I need. 如果启用,则WifiManager.getConnectionInfo()返回带有SSID和我需要的其他信息的WifiInfo对象。 But how can I know if the device has already connected to the hotspot with that SSID, or still connecting? 但是如何知道设备是否已经使用该SSID连接到热点,或者仍在连接? Do I have to create a BroadcastReciever class? 我必须创建一个BroadcastReciever类吗?

   public  boolean IsWiFiConnected(){
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return netInfo.isConnectedOrConnecting();
}

Here is a code for you: 这是给您的代码:

public boolean isInternetAvailable() {
    ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null)
        return (cm.getActiveNetworkInfo().isConnected() && cm.getActiveNetworkInfo().isAvailable());
    else
        return false;
}

Hope it will help you. 希望对您有帮助。

this may helps you 这可能对您有帮助

public static boolean isCheckedNetwork() {

        ConnectivityManager connect = (ConnectivityManager) mcontext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = null;
        if (connect != null) {
            network = connect.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        }
        return network == null ? false : network.isConnected();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM