This post may seem long.. But it’s very simple. So please please help. Thanks.
In short words, Whenever there is Connectivity change from Wifi to Mobile, I receive a message in my Broadcast receiver 2 times(First time NO Network is found). And when I move from Mobile to Wifi, I receive it three times. Can anyone please explain me this strange behavior.
I have implemented a code using IntentFilterConnectivityManager.CONNECTIVITY_ACTION) and registered it to my receiver.
IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver , mNetworkStateFilter);
private final BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
//The Network State has Been Changed
Log.d(TAG, "CHANGE HAPPENED" );
if (networkInfo != null)
{
Log.d(TAG, "Connected To: " + networkInfo.getTypeName());
Log.d(TAG, "NUMBER: " + networkInfo.getType());
Log.d(TAG, "isConnectedOrConnecting: " + networkInfo.isConnectedOrConnecting());
Log.d(TAG, "isConnected: " + networkInfo.isConnected());
Log.d(TAG, "isFailover: " + networkInfo.isFailover());
Log.d(TAG, "Reason :" + networkInfo.getReason());
Log.d(TAG, "Details " + networkInfo.getDetailedState() );
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
Log.d(TAG, "noConnectivity: " + noConnectivity);
Log.d(TAG, "reason: " + reason);
Log.d(TAG,"DONE");
}
}
};
I have observed the following behavior. Can anyone please explain the reason for this happening.
Initially My Device is connected to Wifi.
The APK is now Deployed on the Device.
My Logcat entry is
D/MainActivity(32005): CHANGE HAPPENED
D/MainActivity(32005): Connected to WIFI
This is understandable.
Now the Router is turned off. So now the device attempts to connect to 4G.
Now, the Logcat Entry is as follows.
D/MainActivity(32005): CHANGE HAPPENED
D/MainActivity(32005): CHANGE HAPPENED
D/MainActivity(32005): Connected to Mobile
May be this can be attributed to the fact the first the connection is broken, which causes CHANGE HAPPENED to be printed but not the Message “Connected to “. And then the connection is established with Mobile resulting in another CHANGE HAPPENED with Connected to Mobile.
Now the router is turned on again.
But this time the logcat reports,
D/MainActivity(32005): CHANGE HAPPENED
D/MainActivity(32005): Connected to WIFI
D/MainActivity(32005): CHANGE HAPPENED
D/MainActivity(32005): Connected to WIFI
D/MainActivity(32005): CHANGE HAPPENED
D/MainActivity(32005): Connected to WIFI
I am not able to understand why the Logcat reported this message three times. Can anyone please help me understand this behavior.
This is my manisfest file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wififourghandoff"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
THIS IS MY OBSERVATION.
Is the Sequence like the Following :
Wifi to Mobile :
1. Network Info is NULL
2. Netowrk Info object Exists and the “reason(networkInfo.getReason()” is dataEnabled.
From Mobile to Wifi :
1. Broadcast Message Received.
2. Netowrk Info object Exists and the “reason(networkInfo.getReason()” is dataDisabled.
3. NetworkInfo with Exists withe “reason(networkInfo.getReason()” as null
MY Device is Camped on to WIFI Initially
My Logcat
D/MainActivity( 880): CHANGE HAPPENED
D/MainActivity( 880): Connected To: WIFI
D/MainActivity( 880): NUMBER: 1
D/MainActivity( 880): isConnectedOrConnecting: true
D/MainActivity( 880): isConnected: true
D/MainActivity( 880): isFailover: false
D/MainActivity( 880): Reason :null
D/MainActivity( 880): Details CONNECTED
D/MainActivity( 880): noConnectivity: false
D/MainActivity( 880): reason: null
D/MainActivity( 880): DONE
NOW WIFI is Turned OFF. So Now My device moves into MOBILE. Following is the Logcat.
D/MainActivity( 880): CHANGE HAPPENED
D/MainActivity( 880): CHANGE HAPPENED
D/MainActivity( 880): Connected To: mobile
D/MainActivity( 880): NUMBER: 0
D/MainActivity( 880): isConnectedOrConnecting: true
D/MainActivity( 880): isConnected: true
D/MainActivity( 880): isFailover: true
D/MainActivity( 880): Reason :dataEnabled
D/MainActivity( 880): Details CONNECTED
D/MainActivity( 880): noConnectivity: false
D/MainActivity( 880): reason: dataEnabled
D/MainActivity( 880): DONE
NOW my WIFI is turned on. And my device camps back to Wifi.
D/MainActivity( 880): CHANGE HAPPENED
D/MainActivity( 880): Connected To: WIFI
D/MainActivity( 880): NUMBER: 1
D/MainActivity( 880): isConnectedOrConnecting: true
D/MainActivity( 880): isConnected: true
D/MainActivity( 880): isFailover: false
D/MainActivity( 880): Reason :null
D/MainActivity( 880): Details CONNECTED
D/MainActivity( 880): noConnectivity: false
D/MainActivity( 880): reason: null
D/MainActivity( 880): DONE
D/MainActivity( 880): CHANGE HAPPENED
D/MainActivity( 880): Connected To: WIFI
D/MainActivity( 880): NUMBER: 1
D/MainActivity( 880): isConnectedOrConnecting: true
D/MainActivity( 880): isConnected: true
D/MainActivity( 880): isFailover: false
D/MainActivity( 880): Reason :null
D/MainActivity( 880): Details CONNECTED
D/MainActivity( 880): noConnectivity: false
D/MainActivity( 880): reason: dataDisabled
D/MainActivity( 880): DONE
D/MainActivity( 880): CHANGE HAPPENED
D/MainActivity( 880): Connected To: WIFI
D/MainActivity( 880): NUMBER: 1
D/MainActivity( 880): isConnectedOrConnecting: true
D/MainActivity( 880): isConnected: true
D/MainActivity( 880): isFailover: false
D/MainActivity( 880): Reason :null
D/MainActivity( 880): Details CONNECTED
D/MainActivity( 880): noConnectivity: false
D/MainActivity( 880): reason: null
D/MainActivity( 880): DONE
Thanks & Regards,
Yuvi
I think if you log the
Extracontent the behavior might make more sense:http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION
My Interpretation of what is going on with the three events when WiFi is enabled:
disabled and that there is another available network (WiFi). This
is the event that has the NetworkInfo.
So, it’s a little redundant for you, but this is normal behavior for the Android. Here are some logs from my experiment: