android.net.wifi.STATE_CHANGE:Wifi 断开连接时未触发

发布于 2024-10-23 20:11:58 字数 201 浏览 1 评论 0原文

当 Wifi 连接恢复时,仅具有操作 NETWORK_STATE_CHANGED_ACTION(其常量值为 android.net.wifi.STATE_CHANGE)的广播意图是否正常?即当 Wifi 断开时我没有得到这个意图。

更新:我最感兴趣的是 >= 2.2 Froyo

Is it normal to only have a broadcast intent with action NETWORK_STATE_CHANGED_ACTION (whose constant value is android.net.wifi.STATE_CHANGE) when a Wifi connection is coming back up? I.e. I don't get this intent when Wifi is being disconnected.

UPDATE: I am mostly interested to >= 2.2 Froyo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人事已非 2024-10-30 20:11:58

公共静态最终字符串
SUPPLICANT_CONNECTION_CHANGE_ACTION

自:API 级别 1

广播意图
表示连接到的操作
请求者已成立
(现在可以执行
Wi-Fi 操作)或连接到
请求者已经迷失。一
extra 提供的连接状态为
布尔值,其中 true 表示已连接。

另请参阅

EXTRA_SUPPLICANT_CONNECTED

常量值:“android.net.wifi.supplicant.CONNECTION_CHANGE”

在 android 的 API 中,它表示检查 STATE_CHANGE 的网络连接不是一个好主意,而您应该使用 SUPPLICANT_CONNECTION_CHANGE_ACTION。这将注意到 wifi 网络的建立以及 wifi 网络的断开。我不知道这是否对你有帮助,但我希望如此。 LINK

public static final String
SUPPLICANT_CONNECTION_CHANGE_ACTION

Since: API Level 1

Broadcast intent
action indicating that a connection to
the supplicant has been established
(and it is now possible to perform
Wi-Fi operations) or the connection to
the supplicant has been lost. One
extra provides the connection state as
a boolean, where true means CONNECTED.

See Also

EXTRA_SUPPLICANT_CONNECTED

Constant Value: "android.net.wifi.supplicant.CONNECTION_CHANGE"

In android's API it says that it's not a good idea to check STATE_CHANGE for network connectivity and instead you should use SUPPLICANT_CONNECTION_CHANGE_ACTION. this will notice an establishment to a wifi network, and the disconnection of a wifi network. I don't know if this might help you, but I do hope so. LINK

迷迭香的记忆 2024-10-30 20:11:58

我在我的项目中有类似的需求,最终不得不使用两者。

android.net.wifi.supplicant.CONNECTION_CHANGE 操作在网络连接时发送广播,但通常在设备具有 IP 地址之前,因此我需要 android.net.wifi.STATE_CHANGE 操作。

仅当设备与网络断开连接但 wifi 仍处于启用状态(例如,当热点超出范围时)时, android.net.wifi.STATE_CHANGE 操作才会在断开连接时接收广播,

因此您应该将接收器的两个操作放入清单:

<receiver android:name="net.moronigranja.tproxy.WifiReceiver">
            <intent-filter>
                    <action android:name="android.net.wifi.STATE_CHANGE"/>
                    <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
            </intent-filter>
</receiver>

然后添加一个 if 来检查意图中正在调用哪个操作。这是我的代码中的 BroadcastReceiver 的 onReceive 方法:

public void onReceive(Context c, Intent intent) {
      if(intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)){ 
          boolean connected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
          if(!connected) {
               //Start service for disconnected state here
          }
      }

      else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
          NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
          if( netInfo.isConnected() )
          {
              //Start service for connected state here.
          }   
      }
  }

I had a similar need in my project and ended up having to use both.

The android.net.wifi.supplicant.CONNECTION_CHANGE action sends a broadcast when the network is connected, but usually before the device has an IP address, so I needed the android.net.wifi.STATE_CHANGE action for that.

The android.net.wifi.STATE_CHANGE action receives a broadcast on disconnect only if the device is disconnecting from a network, but wifi is still enabled (when hotspot goes out of range, for example)

So you should put both actions for the receiver in the manifest:

<receiver android:name="net.moronigranja.tproxy.WifiReceiver">
            <intent-filter>
                    <action android:name="android.net.wifi.STATE_CHANGE"/>
                    <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
            </intent-filter>
</receiver>

and you put an if to check which action is being called in the intent. Here is the onReceive method of the BroadcastReceiver in my code:

public void onReceive(Context c, Intent intent) {
      if(intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)){ 
          boolean connected = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
          if(!connected) {
               //Start service for disconnected state here
          }
      }

      else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
          NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
          if( netInfo.isConnected() )
          {
              //Start service for connected state here.
          }   
      }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文