A
A
Alexander Popov2016-02-01 12:10:52
Android
Alexander Popov, 2016-02-01 12:10:52

Why does onReceive immediately fire after registerReceiver?

Good afternoon.
Actually, the question is in the header,
as soon as I register the receiver to track changes in the connection to the Internet like this:

receiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                if (NivaUtils.isConnected(context)) {
                    final ProgressDialog progress = NivaUtils.showProgressDialog(getActivity());
                    ...
                }
            }
        };
getActivity().registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))

it immediately executes the onReceive method.
ps Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Dovnar, 2016-02-03
@SolidlSnake

Receivers are generally crazy in terms of calls.
It is better to always check that the call occurred exactly for the action you need.

if(intent.getAction().equals("SOME_ACTION") {

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question