66.5 Starting the Service
Now that the service has been implemented and declared in the manifest file, the next step is to add code to start the service when the button is clicked. Locate and load the MainActivity.kt file into the editor and implement the buttonClick() method to add the code to start the service:
package com.ebookfrenzy.serviceexample
.
.
import android.view.View
import androidx.core.app.JobIntentService.enqueueWork
class MainActivity : AppCompatActivity() {
val SERVICE_ID = 1001
.
.
fun buttonClick(view: View)
{
enqueueWork(this, MyJobIntentService::class.java, SERVICE_ID, intent)
}
}
The code declares a service id (this can be any integer value) which is used to identify the service. Next, the buttonClick() method is implemented to call enqueueWork() method...