更改WIFI的广播动作 [英] Broadcast action for WIFI change

查看:148
本文介绍了更改WIFI的广播动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,无论何时设备与WIFI网络连接或断开连接,我都必须得到通知.为此,我必须使用BroadcastReceiver,但是在阅读了本文的其他文章和问题后,我有点困惑,应该为此使用哪种广播动作.我认为我有三种选择:

In my application I have to get notified whenever the device connects or disconnects from a WIFI network. For this I have to use a BroadcastReceiver but after reading through different articles and questions here on SO I'm a bit confused which Broadcast action I should use for this. In my opinion I have three choices:

  • SUPPLICANT_CONNECTION_CHANGE_ACTION
  • NETWORK_STATE_CHANGED_ACTION
  • CONNECTIVITY_ACTION

为减少资源,我真的只想在设备CONNECTED进入WIFI网络(并且已接收到IP地址)或设备从CONNECTED收到DISCONNECTED时得到通知.我不在乎其他国家,例如CONNECTING等.

To reduce resources I really only want to get notified whenever the device is CONNECTED to a WIFI network (and it has received an IP address) or when the device has DISCONNECTED from one. I do not care about the other states like CONNECTING etc.

那么您认为我应该为此使用的最佳广播动作是什么?我是否必须手动过滤onReceive中的事件(因为我收到的内容比CONNECTEDDISCONNECTED更多)?

So what do you think is the best Broadcast action I should use for this? And do I have to manully filter the events (because I receieve more then CONNECTED and DISCONNECTED) in onReceive?

正如我在下面的评论中指出的那样,我认为SUPPLICANT_CONNECTION_CHANGE_ACTION对我来说将是最佳选择,但它不会被我的应用程序激发或接收. 其他对于此广播也有相同的问题,但从来没有真正的解决方案建议(实际上使用了其他广播).有什么想法吗?

As I pointed out in a comment below I think SUPPLICANT_CONNECTION_CHANGE_ACTION would be the best choice for me but it is never fired or received by my application. Others have the same problem with this broadcast but a real solution for this is never proposed (in fact other broadcasts are used). Any ideas for this?

推荐答案

您可以使用 WifiManager.NETWORK_STATE_CHANGED_ACTION可以正常工作.

在适合您的清单,片段或活动中使用WifiManager.NETWORK_STATE_CHANGED_ACTION操作注册接收器.

Register receiver with WifiManager.NETWORK_STATE_CHANGED_ACTION Action, either in Manifest or Fragment or Activity, which ever suited for you.

覆盖接收者:

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

    final String action = intent.getAction();

    if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
        NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        boolean connected = info.isConnected();
        if (connected)
        //call your method
    }      
}

这篇关于更改WIFI的广播动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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