Listening for network events

The last improvement we want is the ability for our application to execute synchronization when connectivity is established. Create a new class called in the same NetworkReceiver package. Make sure you have the implementation like this:

    class NetworkReceiver : BroadcastReceiver() {      private val tag = "Network receiver"      private var service: MainService? = null         private val serviceConnection = object : ServiceConnection { 
        override fun onServiceDisconnected(p0: ComponentName?) { 
          service = null 
        } 
 
        override fun onServiceConnected(p0: ComponentName?, binder: IBinder?) { if (binder is MainService.MainServiceBinder) { service = binder.getService() service?.synchronize() } } } override fun onReceive(context: Context?, p1: ...

Get Mastering Android Development with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.