Starting the service

To start the service, we need to define the intent that will represent it. This is an example on how the service can be started:

    val startServiceIntent = Intent(ctx, MainService::class.java) 
    ctx.startService(startServiceIntent) 

Here, ctx represents any valid instance of the Android Context class.

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.