kotlin android start new service

Viewed 16557

By using the below code, I'm trying to start new service from a Broadcast receiver, but the service does not seem to start. What is the correct way to start a service in kotlin?

val intent = Intent(context, LocationService::class.java)
 if (context != null) {
      context.startService(intent)
 }
4 Answers

Use safe access compression in kotlin:

val intent = Intent(context, LocationService::class.java)
context?.startService(intent)

Also define your service in your manifest.

Related