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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.