kotlin android start new service

Logo picture Logo · Aug 9, 2017 · Viewed 14.8k times · Source

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)
 }

Answer

AskNilesh picture AskNilesh · Aug 9, 2017

try this

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

And don't forget to register your service in manifest file

 <service  android:name="packageName.LocationService"/>