I am able to detect whether the application is connected to WiFi or mobile network.But I am not able to log the number of times the internet connection changes from Wifi to mobile network or vice versa.To log the network change do i need to check whether it is connected to WiFi or mobile.Then later how i need to check that application connected from WiFi to Mobile or vice versa?
I want to count the number of times the internet connectivity changes that occur in a android application.
The code snippet I have written is { public void onReceive(final Context context, final Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
SharedPreferences pref =this.getApplicationContext.getSharedPreferences("network_change_count", Context.MODE_PRIVATE);
int count = pref.getInt("networkchange", 0);
pref.edit().putInt("networkchange", ++count).apply();
// return pref.getInt("networkchange", count);
}
} }
I have registered the receiver in the manifest file. But now I am getting nullPointerexception when i try to invoke get the shared preference value.
SharedPreferences pref =this.getApplicationContext.getSharedPreferences("network_change_count", Context.MODE_PRIVATE); int connectivitychange=pref.getInt("networkchange", 0);
May i Know what is the mistake which I have done in this code?
Please give me Suggestions:)