-6

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:)

padma
  • 15
  • 6

1 Answers1

0

Try this method.

registerReceiver(networkBroadcastReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));

You will get call here once connectivity changed.

BroadcastReceiver networkBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        count++;
    }
};
  • can i get a link which expalins the above code clearly as i am a beginner i cannot understand so easily – padma Jan 23 '18 at 05:08
  • refer this. hope it helps http://www.androhub.com/android-detect-internet-connection-using-broadcast-receiver/ – Muhammed Arshad K Jan 23 '18 at 05:19
  • arshad Where i need to include this statement /*registerReceiver(networkBroadcastReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));*/ – padma Jan 23 '18 at 07:24
  • registerReceiver in onStart and unRegister in onStop – Muhammed Arshad K Jan 23 '18 at 07:26
  • arshad i can use this register method in activity also ? if i do this the count wil increase only once then later it comes out of loop – padma Jan 23 '18 at 09:41
  • You can. Whenever the network change is detected, this method will call and the count will increase – Muhammed Arshad K Jan 23 '18 at 09:43
  • arshad here no need to write code to detect any change in network kn? – padma Jan 23 '18 at 10:25
  • can we save the number of connectivity change in a shared preference? – padma Mar 12 '18 at 06:52
  • Yes, you can save it – Muhammed Arshad K Mar 12 '18 at 07:02
  • arshad why the value in shared preference is incrementing twice? what might be the problem? – padma Mar 26 '18 at 06:41
  • Please debug and check it. May be you are missing something – Muhammed Arshad K Mar 27 '18 at 05:03
  • SharedPreferences pref =context.getSharedPreferences("network_change_count", Context.MODE_PRIVATE); SharedPreferences.Editor editor=pref.edit(); incrementedNetworkCount = incrementedNetworkCount+1; pref.edit().putInt("networkchange",incrementedNetworkCount).apply(); editor.putInt("networkchange",incrementedNetworkCount); editor.commit(); – padma Mar 27 '18 at 06:49