Not able to receive network related events

4k views Asked by At

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.

4

There are 4 answers

1
Tofeeq Ahmad On BEST ANSWER

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.

0
jjNford On

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.

1
PC. On

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

0
Ivan Bartsov On

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.