如何检查是否WiFi是真正连接的Andr​​oid [英] how to check if wifi is really connected in Android

查看:160
本文介绍了如何检查是否WiFi是真正连接的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的Andr​​oid设备连接到WiFi热点。
我创建了一个新的 wificonfiguration ,并将其添加到 wifimanager ,这个 wificonfiguration NETWORKID 。然后我调用函数 wifi.enableNetwork(NETWORKID,真)。

I'd like my android device to connect to a wifi hotspot. I created a new wificonfiguration and add it into the wifimanager, this wificonfiguration has NetworkId.Then I invoke the function wifi.enableNetwork(NetworkId, true).

在这之后,我想恳求将通过获取IP地址,认证,最后物理连接到热点。
那么,有没有一种方法,以确定是否WiFi是物理连接或不?

After that, I think the supplicant will go through obtaining ip address, authentication, and at last physically connect to the hotspot. So is there a way to identify if the wifi is physically connected or not?

我想preFER处理程序一样的方法。

I would prefer a handler-like method.

推荐答案

您可以试试这个:

ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (wifi.isConnected()) {
    // Your code here
}

编辑:更多细节:

More details:

注册一个广播接收器在你的清单像这样:

Register a BroadcastReceiver in your manifest like so:

<receiver android:name="WifiReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        <action android:name="android.net.wifi.STATE_CHANGE"/>
    </intent-filter>
</receiver>

然后把code以上的接收器上的像这样的的onReceive()方法:

@Override
public void onReceive(Context context, final Intent intent) {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (wifi.isConnected()) {
        // Your code here
    }
}

这篇关于如何检查是否WiFi是真正连接的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆