Get a "callback" for (losing) changing the connection in the service

Features: I have a service that launches notification controls (CastCompanionLibrary). When I lose connection, the mailing manager doesn't try to quickly drain the service. Therefore, if the user falls asleep the device within a couple of seconds after the loss of connection, the notification is not removed.

General question: How can I get the connection change callback / warning in the service?

+3


source to share


1 answer


You can programmatically create BroadcastReceiver

in Service

which the ConnectivityManager.CONNECTIVITY_ACTION activity listens by calling registerReceiver () when you want to start listening (say, in onCreate()

or when you show a notification that you ultimately want to hide), then calling unregisterReceiver () when you want to stop listening (for example, at onDestroy()

or when the notification is removed).

Then you can restore the connection using code like



boolean isDisconnected = intent.getBooleanExtra(
            ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

      

in your method onReceive()

.

+3


source







All Articles