如何查看 Android 上的 Wi-Fi 是否已连接?
我什至不希望我的用户尝试下载某些内容,除非他们连接了 Wi-Fi。然而,我似乎只能判断是否启用了 Wi-Fi,但他们仍然可以有 3G 连接。
android.net.wifi.WifiManager m = (WifiManager) getSystemService(WIFI_SERVICE);
android.net.wifi.SupplicantState s = m.getConnectionInfo().getSupplicantState();
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(s);
if (state != NetworkInfo.DetailedState.CONNECTED) {
return false;
}
然而,这个状态并不是我所期望的。即使 Wi-Fi 已连接,我仍收到 OBTAINING_IPADDR
状态。
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 still have a 3G connection.
android.net.wifi.WifiManager m = (WifiManager) getSystemService(WIFI_SERVICE);
android.net.wifi.SupplicantState s = m.getConnectionInfo().getSupplicantState();
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(s);
if (state != NetworkInfo.DetailedState.CONNECTED) {
return false;
}
However, the state is not what I would expect. Even though Wi-Fi is connected, I am getting OBTAINING_IPADDR
as the state.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
您应该能够使用 ConnectivityManager 来获取 Wi-Fi 适配器的状态。从那里您可以检查它是否已连接甚至可用。
注意:需要注意的是(对于我们这里的新手来说),您需要添加
到
AndroidManifest.xml 中才能使其正常工作。
注意2:
公共网络信息getNetworkInfo (int networkType)
现已弃用:注意3:
公共静态最终int TYPE_WIFI
现已弃用:
You should be able to use the ConnectivityManager to get the state of the Wi-Fi adapter. From there you can check if it is connected or even available.
NOTE: It should be noted (for us n00bies here) that you need to add
to your
AndroidManifest.xml for this to work.
NOTE2:
public NetworkInfo getNetworkInfo (int networkType)
is now deprecated:NOTE3:
public static final int TYPE_WIFI
is now deprecated:由于方法 NetworkInfo.isConnected() 现已在 API-23 中弃用,因此这里有一个方法可以检测 Wi-Fi 适配器是否已连接打开并使用 WifiManager 连接到接入点:
Since the method NetworkInfo.isConnected() is now deprecated in API-23, here is a method which detects if the Wi-Fi adapter is on and also connected to an access point using WifiManager instead:
我只是使用以下内容:
它将在您调用 getSupplicantState() 时返回这些状态之一;
I simply use the following:
Which will return one of these states at the time you call getSupplicantState();
我在我的应用程序中使用它来检查活动网络是否为 Wi-Fi:
I am using this in my apps to check if the active network is Wi-Fi:
我查看了一些这样的问题,并提出了这个:
我在 Root Toolbox PRO 中使用 if 进行许可证检查,它似乎工作得很好。
I had a look at a few questions like this and came up with this:
I use if for my license check in Root Toolbox PRO, and it seems to work great.
自 API 起,
NetworkInfo
类已弃用级别 29,以及相关的访问方法,例如ConnectivityManager#getNetworkInfo()
和ConnectivityManager#getActiveNetworkInfo()
。文档现在建议人们使用
ConnectivityManager.NetworkCallback
API 用于异步回调监控,或使用ConnectivityManager#getNetworkCapability
或ConnectivityManager#getLinkProperties< /code>
用于同步访问网络信息
要检查 WiFi 是否已连接,这是我使用的代码:
Kotlin:
Java:
请记住还要添加权限
ACCESS_NETWORK_STATE
到您的清单文件。The
NetworkInfo
class is deprecated as of API level 29, along with the related access methods likeConnectivityManager#getNetworkInfo()
andConnectivityManager#getActiveNetworkInfo()
.The documentation now suggests people to use the
ConnectivityManager.NetworkCallback
API for asynchronized callback monitoring, or useConnectivityManager#getNetworkCapabilities
orConnectivityManager#getLinkProperties
for synchronized access of network informationTo check if WiFi is connected, here's the code that I use:
Kotlin:
Java:
Remember to also add permission
ACCESS_NETWORK_STATE
to your Manifest file.虽然 Jason 的答案是正确的,现在 getNetWorkInfo (int) 已被弃用。因此,下一个函数将是一个不错的选择:
While Jason's answer is correct, nowadays getNetWorkInfo (int) is a deprecated method. So, the next function would be a nice alternative:
许多答案使用已弃用的代码或更高 API 版本上可用的代码。现在我用这样的东西
Many of answers use deprecated code, or code available on higer API versions. Now I use something like this
以下代码(Kotlin 中)适用于 API 21 直到至少当前 API 版本 (API 29)。
函数 getWifiState() 返回 WiFi 网络状态的 3 个可能值之一:
在枚举类中定义的禁用、EnabledNotConnected 和 Connected。
这样可以做出更精细的决策,例如通知用户启用 WiFi,或者如果已启用,则连接到可用网络之一。
但是,如果需要的只是一个指示 WiFi 接口是否连接到网络的布尔值,那么另一个函数 isWifiConnected() 将为您提供该值。它使用前一个结果并将结果与“已连接”进行比较。
它受到之前一些答案的启发,但试图解决 Android API 的演变或 IP V6 的可用性缓慢增加所带来的问题。
诀窍是使用:
而不是:
根据文档: https://developer.android.com/reference/kotlin/android/net/ wifi/WifiInfo.html#getbssid
如果未连接到网络,它将返回 null。即使我们没有权限获取实际值,如果我们已连接,它仍然会返回 null 以外的值。
还要记住以下几点:
在清单中,不要忘记添加:
建议的代码是:
The following code (in Kotlin) works from API 21 until at least current API version (API 29).
The function getWifiState() returns one of 3 possible values for the WiFi network state:
Disable, EnabledNotConnected and Connected that were defined in an enum class.
This allows to take more granular decisions like informing the user to enable WiFi or, if already enabled, to connect to one of the available networks.
But if all that is needed is a boolean indicating if the WiFi interface is connected to a network, then the other function isWifiConnected() will give you that. It uses the previous one and compares the result to Connected.
It's inspired in some of the previous answers but trying to solve the problems introduced by the evolution of Android API's or the slowly increasing availability of IP V6.
The trick was to use:
instead of:
According to the documentation: https://developer.android.com/reference/kotlin/android/net/wifi/WifiInfo.html#getbssid
it will return null if not connected to a network. And even if we do not have permission to get the real value, it will still return something other than null if we are connected.
Also have the following in mind:
In the Manifest, do not forget to add:
Proposed code is:
这适用于 Q 之前和之后的 Android 设备
This works for Android devices before and after Q
使用
WifiManager
您可以执行以下操作:方法 getNeworkId仅在未连接网络时返回-1;
Using
WifiManager
you can do:The method getNeworkId returns -1 only when it's not connected to a network;
尝试一下这个方法。
此方法将有助于查找可用或不可用的互联网连接。
Try out this method.
This method will help to find internet connection available or not.
这对我有用:
并添加此权限:
This works for me:
And add this permission:
与 @Jason Knight 的答案类似,但采用 Kotlin 方式:
Similar to @Jason Knight answer, but in Kotlin way:
以下是我在应用程序中用作实用方法的方法:
Here is what I use as a utility method in my apps:
在新版本的Android中
也添加了权限
In new version Android
and add premission too
这是一个更简单的解决方案。请参阅堆栈溢出
问题检查 Android 上是否启用 Wi-Fi< /em>.
PS 不要忘记将代码添加到manifest.xml 文件中以允许权限。如下图所示。
This is an easier solution. See Stack Overflow
question Checking Wi-Fi enabled or not on Android.
P.S. Do not forget to add the code to the manifest.xml file to allow permission. As shown below.
Try
This 返回 0,直到设备具有可用的连接(在我的机器上,三星 SM-T280,Android 5.1.1)。
Try
This returns 0 until the device has a usable connection (on my machine, a Samsung SM-T280, Android 5.1.1).
如果没有开启WIFI,可以按如下方式打开WIFI
1. 检查 WIFI 状态(@Jason Knight 回答)
2.如果没有激活,则激活它
不要忘记在清单文件中添加 WIFI 权限
你的 Java 类应该是这样的
}
You can turn WIFI on if it's not activated as the following
1. check WIFI state as answered by @Jason Knight
2. if not activated, activate it
don't forget to add WIFI permission in the manifest file
Your Java class should be like that
}
对于 JAVA 添加此:
在 Manifest 文件中添加以下权限:
Add this for JAVA:
in Manifest file add the following permissions:
有点老的问题,但这就是我使用的。要求最低 api 级别 21 还考虑已弃用的 Networkinfo api。
Kind of old a question but this is what i use. requires min api level 21 also takes in consideration deprecated Networkinfo apis.
这适用于最新版本的 android:
并且在清单中:
This works with the latest versions of android:
And in the manifest: