从未收到SUPPLICANT_CONNECTION_CHANGE_ACTION [英] SUPPLICANT_CONNECTION_CHANGE_ACTION never received

查看:143
本文介绍了从未收到SUPPLICANT_CONNECTION_CHANGE_ACTION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在设备切换网络时收到通知,但出于某种奇怪的原因,尽管有SO上的文档和各种示例,但该特定操作从未发送到我的接收器.

I am looking to be notified when the device switches networks but for some oddball reason, in spite of the documentation and various examples on SO, this particular action never gets sent to my receiver.

这是我的代码

收据:

BroadcastReceiver connectedToLocalWifiReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            Log.d(TAG, "ACTION='" + action +"'"); //<-- WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION never turns up here
            if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
                if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
                    //this block of code is never called

                } else {
                    // ditto for this block
                }
            }
        }
    };

注册: 在连接网络之前,用我的代码注册接收器.我尝试将其注册到其他动作中,但它们确实出现了(例如,像ConnectivityManager.CONNECTIVITY_ACTION),但是对于SUPPLICANT_CONNECTION_CHANGE_ACTION,我什么也没得到.

REGISTERING: In my code right before I go to connect to the network I register my receiver. I have tried registering it with other actions and they do turn up (like ConnectivityManager.CONNECTIVITY_ACTION for instance) but for SUPPLICANT_CONNECTION_CHANGE_ACTION I get nothing.

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
registerReceiver(connectedToLocalWifiReceiver, intentFilter);
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();

因此,在这一点上,wifi实际上确实断开了其当前网络的连接,并连接到我指定的那个,但是这样做并没有广播任何内容.

So at this point the wifi does in fact disconnect from its current network and connect to the one I'm specifying, but it does so without ever broadcasting anything.

我看不到任何明显错误的内容,但必须缺少一些非常基本的内容.

I can't see anything obviously wrong, but must be missing something pretty fundamental.

我的清单权限是这样设置的,尽管我不知道如何设置,因为它实际上可以正确切换wifi,权限可能与它有任何关系:

My manifest permissions are set thusly, though I don't see how, given that it actually does the wifi switching correctly, permissions could have anything to do with it anymore:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

很乐意找出我忽略的地方.

Would love to find out what I'm overlooking.

推荐答案

我遇到了类似的问题,并设法解决了这个问题.

I stuck with a similar problem and managed to solve it.

我使用的是NETWORK_STATE_CHANGED_ACTION而不是SUPPLICANT_CONNECTION_CHANGE_ACTION.

registerReceiver(new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        Log.v(LOG_TAG, "onReceive");
        if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            Log.d(LOG_TAG, "network state was changed");
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                Log.d(LOG_TAG, "network connection has been established");
                // the receiver is no longer needed, so unregist it immediately.
                unregisterReceiver(this);

                // do something...

            }
        }
    }
}, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));

注意:我没有设置任何其他权限.

Note: I don't set any additional permissions.

这篇关于从未收到SUPPLICANT_CONNECTION_CHANGE_ACTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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