I'm registering a receiver to capture the ConnectivityManager.CONNECTIVITY_ACTION in code i.e. I am not registering it in the application manifest. All is working fine but I notice that I automatically receive a broadcast as soon as I register my receiver and despite the network being already on.
My question is can I rely on this automatic broadcast to check whether the network is available or not i.e. not explicitly call methods to check the network?
I was only expecting a broadcast when a network changed and not upon registering my receiver, however the fact that I do receive a broadcast is very useful but can I rely on it. Is this a documented action?
Yes, that's called sticky broadcasts. Sticky broadcasts are sent to receiver as soon as registerBroadcast
is called.
来源:https://stackoverflow.com/questions/6670514/connectivitymanager-connectivity-action-always-broadcast-when-registering-a-rec