试图探测时Wifi网络连接的Andr​​oid时,怪异的行为 [英] Weird behavior when trying to detect when Wifi network connected android

查看:332
本文介绍了试图探测时Wifi网络连接的Andr​​oid时,怪异的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做一个相对简单的任务在Android中。

I am currently trying to do a relatively simple task in Android.

我想检测时,无线网络的全连通。在这一刻我不在乎是否有互联网连接,我只是想知道确切的时刻,当我的设备被认为是连接到网络。

I want to detect when a wireless network is fully connected. At this moment I do not care whether there is internet connection, I just want to know the exact moment when my device is considered connected to network.

我会尽力来形容我做什么,以及如何凄惨了我失败了。

I will try to describe what I did, and how miserably had I failed.

裸记住,我已经试过官方建议,以及一些答案在这里,但无济于事。

Bare in mind that I have tried official recommendations as well as some answers here but to no avail.

活动寄存器接收器:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(ReceiverClass,intentFilter);

在我接收我试图从东西这个回答

In my receiver I have tried stuff from this answer

 NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
  if(info != null) {
     if(info.isConnected()) {
        // Do your work
        Log.d("tag", "connected");
     }
  }

但正如预期的我的应用程序不能正常工作。当我(从上刷卡,并把它关掉)杀了我无线接收广播(明显),但我的两个如果的走线槽和我得到这个登录!
我打开无线网络后,我得到2,一切都连接更多的日志。我已经尝试了两件事像这样

But my app is not working as expected. When I kill Wifi (swipe from top and turn it off) I receive a broadcast (obviously) but both of my If's go trough and I get this logged! After I turn on wifi I get 2 more logs that everything is connected. I have tried a thing or two like this

WifiInfo wi =  intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);

if(wi != null) {
    SupplicantState ss = wi.getSupplicantState();
    Log.d("I am desperate", ss.equals(SupplicantState.COMPLETED));
    }
}

但同样的事情发生。我得到真正的3倍。

but the same stuff happens. I get true 3 times.

我不是在android系统方面的专家,我相信我失去了一些东西相当明显。有我的帮助吗?

I am not an expert in android, I believe I am missing something quite obvious. Any help for me?

更新

我比较多的考虑到这是在Android系统的问题的可能性。此刻,我不再试图摆脱意向信息。

I am more an more considering the possibility that this is an issue in Android system. At the moment I stopped trying to get informations from Intent.

我曾尝试使用 EXTRA_WIFI_STATE 但命令:

Log.d("WifiState" getIntExtra(WifiManager.EXTRA_WIFI_STATE, -9000));

日志我总是为每一个广播我收到默认值(-9000)。

Logs me always the default (-9000) for every single broadcast I receive.

如果我试图<一个href=\"http://developer.android.com/reference/android/net/wifi/WifiManager.html#SUPPLICANT_CONNECTION_CHANGE_ACTION\"相对=nofollow> SUPPLICANT_CONNECTION_CHANGE_ACTION 广播 - 这是从来没有连抓。我真的很困惑在这里...

If I try to SUPPLICANT_CONNECTION_CHANGE_ACTION broadcast - that is never even caught. I am really puzzled here...

推荐答案

我有问题,code波纹管,但我想补充一点,就是帮助了我。

I had problem with code bellow to, but I tried to add something that helped me.

public class WifiReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

    NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    if(info != null) {

        WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        boolean wen = wifi.isWifiEnabled();

        if(info.isConnected()) {
            // Do your work.

            Log.wtf("Wifi", "Connected");

            // e.g. To check the Network Name or other info:
            //WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            //WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            //String ssid = wifiInfo.getSSID();
        }
        else if(!(wen)){
            Log.wtf("Wifi", "No");
        }
    }
}
}

据我注意到,在Android上WIFI将不会立即停用。它有一些子状态一样启用和禁用将登录为启用。

As far as I noticed on Android, Wifi will not disable instantly. It have some sub-states like enabling and disabling that will log as enabled.

这是连接日志记录如预期,但有时它没有断开连接日志记录预期(实际上什么都不记录,至少不是垃圾邮件连接/否)。我查了多种方式无线状态时关闭的,但有时它只是停留在启用(状态3尝试通过getWifiState获得时/ WIFI_STATE_ENABLED())。

This is logging Connected as expected but sometimes it is not logging Disconnected as expected (it's actually logging nothing, at least not spamming Connected/No). I checked on multiple ways Wifi state when turned off but sometimes it just stuck on enabled (state 3/WIFI_STATE_ENABLED when trying to get via getWifiState()).

请我使用非库存,甚至非官方CM11.0 - 4.4.4等等之类禁用无线控件预期并不总是工作它有点窃听

Keep in mind that I'm using Non-Stock and even Non-official CM11.0 - 4.4.4 and it's bit bugged so controls like disable Wifi is not working always as expected.

我不知道没有你设法使其在同时工作,但好运气与它

I don't know did you managed to make it work in meanwhile but good luck with it

这篇关于试图探测时Wifi网络连接的Andr​​oid时,怪异的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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