Android-检测与特定wifi ssid断开连接的正确方法? [英] Android - Correct way to detect disconnecting from a particular wifi ssid?

查看:596
本文介绍了Android-检测与特定wifi ssid断开连接的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些BroadcastReciever示例来检测wifi断开连接,但是它们似乎都无法正常工作(例如,对于每个断开连接触发两次),而且都没有提到对ssid进行检查,这是否可能?

I've seen a couple of BroadcastReciever examples to detect wifi disconnects but none of them seem to work correctly (triggering twice for each disconnect for example) and none mention checking against an ssid, is this even possible?

因此,为了澄清一下,我想检测与特定ssid的断开连接.实际断开连接且未在设备上禁用wifi.

So just to clarify, I want to detect disconnection from a particular ssid. An actual disconnect and not wifi being disabled on the device.

谢谢

重新打开,因为在我们必须测试的设备上 都无效.

Re-opening as nothing works on both the devices we have to test.

推荐答案

NETWORK_STATE_CHANGED_ACTION.卸载另一个应用程序(也正在监听类似事件)时,注册此事件有问题的设备开始工作!不知道一个应用程序如何或为什么会阻止向另一个应用程序注册事件.最终解决方案是;

NETWORK_STATE_CHANGED_ACTION was the answer in the end. The device having the problem registering this event started working when another app (which would also be listening for similar events) was uninstalled! No idea how or why an app could block events registering with another app. The final solution ended up being;

    String action = intent.getAction();

    if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
    {
        WifiManager manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        NetworkInfo.State state = networkInfo.getState();

        if(state == NetworkInfo.State.CONNECTED)
        {
            String connectingToSsid = manager.getConnectionInfo().getSSID().replace("\"", "");
            WifiStateHistory.recordConnectedSsid(connectingToSsid);
//connected
        }

        if(state == NetworkInfo.State.DISCONNECTED)
        {
            if(manager.isWifiEnabled())
            {
                String disconnectedFromSsid = WifiStateHistory.getLastConnectedSsid();
//disconnected
            }
        }
    }

这篇关于Android-检测与特定wifi ssid断开连接的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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