0

I wrote a NetworkStateReceiver for check internet state & publish that intent to Activity via Event Bus.

    public class NetworkStateReceiver extends BroadcastReceiver {
    private static final String TAG = "NetworkStateReceiver";

    NetworkEvent networkEvent = null;

    @Override
    public void onReceive(final Context context, final Intent intent) {
        LogMe.e(TAG, "Network connectivity change");


        if (intent.getExtras() != null && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {

            if (Connectivity.isConnected(context)) {
                LogMe.i(TAG, "Network " + Connectivity.getNetworkInfo(context) + " connected");
                networkEvent = new NetworkEvent(NetworkEvent.INTERNET_CONNECTED, true);

                EventBus.getDefault().post(networkEvent);
            } else {
                LogMe.i(TAG, "Network " + Connectivity.getNetworkInfo(context) + " not connected");
                networkEvent = new NetworkEvent(NetworkEvent.INTERNET_GONE, false);
                EventBus.getDefault().post(networkEvent);

            }
        }
    }
}

In activity i Registered Event Bus onStart and unregistered in onPause.

@Override
protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    broadcastReceiver = new NetworkStateReceiverActivity();
    registerReceiver(broadcastReceiver, intentFilter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
protected void onPause() {
    super.onPause();
    EventBus.getDefault().unregister(this);
    unregisterReceiver(broadcastReceiver);
}


@Subscribe(threadMode = ThreadMode.MAIN)
    public void onNetworkEvent(NetworkEvent event) {

        textView_internet.setText("" + event.isNetworkStateChanged());

        if (event.getTAG() == NetworkEvent.INTERNET_CONNECTED) {

            if (event.isNetworkStateChanged()) {

                LogMe.i(TAG, "Has Internet ....");

                Snackbar.make(getWindow().getDecorView().getRootView(), "Has internet ", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }

        } else if (event.getTAG() == NetworkEvent.INTERNET_GONE) {
            if (!event.isNetworkStateChanged()) {
                LogMe.i(TAG, "No Internet ....");

                Snackbar.make(getWindow().getDecorView().getRootView(), "No internet", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        }
    }

But the problem is Eventbus called all time through internet is connected or not. I want to call the Eventbus when the user changed network state not all time the activity is created. How to get rid off from that? I also tested only with broadcast receiver in activity, but broadcast received every time when activity created.

Md Shihab Uddin
  • 273
  • 4
  • 8
  • Have you tried not to register broadcast for each Activity, but do it in Manifest? Or if you want to have it in Activity, then do some kind of "BaseActivity" which will extends all your Activities. You will register it there, but only once (through some condition). – Tomas Ivan Dec 11 '17 at 08:53
  • My problem : event bus called when activity is created. but i need when the event fired like internet state changed. – Md Shihab Uddin Dec 11 '17 at 08:57

1 Answers1

0

Okay, there are couple of things we have here

  • Your implementation of isNetworkStateChanged() is not entirely correct.

Because basically each time your activity is resumed, you create a new BroadcastReceiver instance, and for this instance, the first time will always be seen as a network change, even if that was not actually the case.

You either need to initialize your BroadcastReceiver with the current network state -in constructor get current network state and set as default-or find another way of not getting a false change first time you register your receiver.

  • The logic in the BroadcastReceiver also is not entirely correct. You are considering being connected as a network change, while being not connected as not a network change. You need to set the network changed flag according to wether the last connected state is the same as the one you are getting or not.

Basically, you need something like,

new NetworkEvent(.., lastConnectionState == newConnectionState);
elmorabea
  • 3,243
  • 1
  • 14
  • 20