Default constructor for IntentService (kotlin)

I am new to Kotlin and a little stack with intent. The manifest shows me an error that my service does not contain a default constructor, but inside the service it looks normal and there are no errors.

Here is my intention for the Services:

class MyService : IntentService { constructor(name:String?) : super(name) { } override fun onCreate() { super.onCreate() } override fun onHandleIntent(intent: Intent?) { } } 

I also tried another option:

 class MyService(name: String?) : IntentService(name) { 

but when I try to start this service, I still get the error message:

 java.lang.Class<com.test.test.MyService> has no zero argument constructor 

Any ideas on fixing the default constructor in Kotlin?

Thanks!

+5
source share
1 answer

As explained here , your class of service should have a consturctor without parameters. Change your implementation to an example:

 class MyService : IntentService("MyService") { override fun onCreate() { super.onCreate() } override fun onHandleIntent(intent: Intent?) { } } 

The Android IntentService documentation says that this name is used only for debugging:

name String : used to indicate a workflow that is important only for debugging.

Unless explicitly stated, on the specified documentation page, the infrastructure should be able to instantiate a class of service and expects a constructor without parameters.

+8
source

Source: https://habr.com/ru/post/1260262/


All Articles