无法接收网络相关事件

发布于 2024-12-27 00:28:37 字数 714 浏览 1 评论 0原文

我已注册到 ConnectivityManager.CONNECTIVITY_ACTION BroadcastReceiver,用于接收网络状态事件,但当我打开或关闭 wifi 连接时,我的 onReceive 函数没有被调用。

正如文档中提到的,这是一个粘性广播接收器,当我们注册它时,它会被触发。

但是当我注册此接收器时,我的 onReceive 函数中没有收到任何事件,可能是什么原因?

在我的清单文件中,我拥有访问 Internet/网络/Wifi 连接及其状态的所有权限。

我使用以下代码注册此意图:

registerReceiver(mNetworkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

在我的 Logcat 中,我收到此广播接收器注册的以下错误消息:

01-01 00:05:29.804: ERROR/WifiHW(1305): Unable to open connection to supplicant on "/data/system/wpa_supplicant/wlan0": Connection refused

可能是什么原因?有什么方法可以查明BroadcastReceiver是否正确注册?

谢谢。

I have registered to ConnectivityManager.CONNECTIVITY_ACTION BroadcastReceiver, for receiving network state events, but my onReceive function is not getting called when I turn on or off my wifi connection.

As mentioned in the docs, this is an Sticky Broadcast Receiver which gets fired when we register for it.

But I am not getting any events in my onReceive function when I register for this receiver, what might be the cause?

In my Manifest file, I have all the permissions for accessing Internet/Network/Wifi connections and their states.

I am registering with this intent using following code:

registerReceiver(mNetworkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

In my Logcat, I get following Error Message register for this broadcast receiver:

01-01 00:05:29.804: ERROR/WifiHW(1305): Unable to open connection to supplicant on "/data/system/wpa_supplicant/wlan0": Connection refused

What might be the cause? Is there any way of finding out whether BroadcastReceiver is registered properly or not?

Thanks.

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

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

发布评论

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

评论(4

脱离于你 2025-01-03 00:28:37

这对我有用:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<receiver android:name=".receiver.ConnectivityReceiver">
   <intent-filter>
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
   </intent-filter>
</receiver>

代码:

public class ConnectivityReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
      Log.d(ConnectivityReceiver.class.getSimpleName(), "action: "
              + intent.getAction());
  }
}

并且不要忘记注册所有网络权限。

This works for me:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<receiver android:name=".receiver.ConnectivityReceiver">
   <intent-filter>
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
   </intent-filter>
</receiver>

Code:

public class ConnectivityReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
      Log.d(ConnectivityReceiver.class.getSimpleName(), "action: "
              + intent.getAction());
  }
}

and do not forget registering all network permission.

依 靠 2025-01-03 00:28:37

清单文件进行尝试

<receiver android:name=".MyNetworkStateReceiver" >
    <intent-filter >
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

使用MyNetworkStateReceiver 类应扩展 BroadcastReceiver

try it using the manifest file

<receiver android:name=".MyNetworkStateReceiver" >
    <intent-filter >
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

the class MyNetworkStateReceiver should extend BroadcastReceiver

忆悲凉 2025-01-03 00:28:37

看起来是一个较低级别的问题 - 谷歌搜索该错误显示多个 ROM 黑客论坛及其 wifi 故障。你在测试什么?尝试在其他设备或模拟器上进行测试。 (我假设您已尝试重新启动您的:))

另外,请确保在您的活动/服务被破坏时取消注册接收器(也许这与连接错误有关)

PS我知道您说过,但仍然仔细检查 你的清单中有

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Looks like a lower-level issue - Googling the error reveals multiple ROM hacking forums and their wifi malfunctions. What're you testing on? Try testing on another device or an emulator. (I assume you have tried rebooting yours :) )

Also, make sure you unregister the receiver when your activity/service gets destroyed (maybe this has something to do with the connection error)

PS I know you said you do, but still double check you have

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in your manifest.

惯饮孤独 2025-01-03 00:28:37

您也可以尝试获取互联网、WIFI和组播锁定的权限,尝试将这些添加到权限中。

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

我需要将这些权限用于我构建的内部应用程序,该应用程序不仅使用 WiFi 发送和接收网络数据,还使用我可以通过 C 语言中的 SDK 过滤的任何类型的网络适配器(LAN、RADIO)(设备必须扎根)。但使用这些权限我没有遇到任何问题。

You may also try getting permissions for the Internet, WIFI and Multicast lock, try adding these to the permissions.

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

I am required to use these permission for an in house application I have built that uses not only the WiFi for sending and receiving network data, but any type of network adapter (LAN, RADIO) that I can filtered through the SDK in C (Device must be rooted). But using these permissions I have no trouble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文