[英]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.