I registered a receiver that listens to network events:
<receiver
android:label="NetworkConnection"
android:name=".ConnectionChangeReceiver" >
<intent-filter >
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
receiver is also very simple:
public class ConnectionChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null) {
Log.v("@@@","Receiver : " + activeNetInfo);
} else {
Log.v("@@@","Receiver : " + "No network");
}
}
}
The problem is, when Wifi is connected I receive 3 identical messages in a row, like this:
Receiver : NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
Receiver : NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
Receiver : NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
They are all "CONNECTED/CONNECTED" (Shouldn't they be something like CONNECTING/OBTAINING_IPADDR, etc.), so the problem is how do I tell when It's really connected? I have some routines that I want to make when wifi is actually connected, and I dont want them to be called three times in a row.
PS: 3G sends only one message, so no problem here.
Update:
Seems like it's device specific problem.
For test I took 2 Desire HD, and 4 random android phones(different Aquos models and some no-name chinese stuff). On both DHD and one random phone on wifi connect I got 3 messages, on remaining phones I got only one message. WTF.
Register your LocalBroadcastreceiver in
oncreate()
itself not inonResume()
. unregistered inonDestroy
Receiving multiple broadcast is a device specific problem. Some phones just send one broadcast while other send 2 or 3. But there is a work around:
Assuming you get the disconnect message when the wifi is disconnected, I would guess the first one is the correct one and the other 2 are just echoes for some reason.
To know that the message has been called, you could have a static boolean that gets toggled between connect and disconnect and only call your sub-routines when you receive a connection and the boolean is true. Something like:
My concern with the approach proposed by Aleksander is that it doesn't consider network changes of the same type, e.g. from one WiFi network to another.
I'm proposing to compare the extraInfo of the active network, which contains the network name, e.g. WiFi SSID or mobile network name like VZW
You can also cache in a static field the last handled connection type and check against the incomming broadcasts. This way you will only get one broadcast per connection type.
When connection type gets changed it will obviously work. When device gets out of connection
activeNetworkInfo
will be null andcurrentType
will beNO_CONNECTION_TYPE
as in default case.I have an application that uploads data when the user comes back online. Since my broadcast receiver can receive the intent multiple times, it can lead to the data being uploaded more than once. To handle this, I use a service that will not do anything if it is already running.
Broadcast Receiver:
Service:
In my case, I was registering my BroadcastReceivers in
onResume
and only unregistering them inonDestroy.
This caused each broadcastreceiver to be registered 3 or 4 times depending on how many times the activity resumes.
Setting your broadcastreceiver in the right place in terms of the activity lifecycle will allow you to stop getting multiple confusing calls.