why OnResume() called repeatedly when I called registerReceiver() in it?

806 views Asked by At

In my app I'm keeping one TCP connection to stream data. I want to implement the following scenario;

I need to listen to Network change, so if network disconnects I need to inform the user about it. If network comes again, I need to automatically connect and start streaming. One more constraint: if app is in Pause state, I should not start streaming. To implement this, I have register a Receiver for CONNECTIVITY_ACTION in my BaseActivity and unregisterReceiver the same in onPause().

If I run this code, onResume() called infinitely. What went wrong? Please help me out.

Thanks in advance!

@Override 
protected void onResume() {
        System.out.println("onResume() in BaseActivity"); 
        registerReceiver(connectivityReceiver, new IntentFilter(
                ConnectivityManager.CONNECTIVITY_ACTION));

        super.onResume();
}

and in onPause()

@Override
    protected void onPause() {
        System.out.println("onPause() in BaseActivity");
        unregisterReceiver(this.connectivityReceiver);
        super.onPause();
    }

Here is my BroadcastReceiver

private BroadcastReceiver connectivityReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

                BaseActivity.this.connectionChange(netCheckerHelper(context));
        }
    };
0

There are 0 answers