2

I want to start an Intent service in kotlin but my service has a parameter i.e. a presenter object. Now we need a default no arg constructor for intent service. How can I achieve that in kotlin?

Is there a way I can overload constructor? I don't think I can pass a default value for the presenter in the constructor

class SongIdentifyService(discoverPresenter : DiscoverPresenter) : IACRCloudListener , IntentService("SongIdentifyService") {

private val callback : SongIdentificationCallback = discoverPresenter
private val mClient : ACRCloudClient by lazy(LazyThreadSafetyMode.NONE) { ACRCloudClient() }
private val mConfig : ACRCloudConfig by lazy(LazyThreadSafetyMode.NONE) { ACRCloudConfig() }
private var initState : Boolean = false
private var mProcessing : Boolean = false


override fun onHandleIntent(intent: Intent?) {

    setUpConfig()
    addConfigToClient()

    startIdentification(callback)
   }

}
3
  • 1
    Welcome to SO, it's a pleasure for us to help you, but we need your code for it Commented Apr 6, 2018 at 17:28
  • 1
    Please, show us your effort code Commented Apr 6, 2018 at 17:34
  • 1
    I have added the code to the question.Thanks Commented Apr 6, 2018 at 18:18

1 Answer 1

2

You can have two secondary constructors like this (simplified):

class SongIdentifyService {
    constructor(discoverPresenter: DiscoverPresenter)
    constructor()
}

Or make discoverPresenter nullable and give it a default value:

class SongIdentifyService(discoverPresenter: DiscoverPresenter? = null)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much @s1m0nw1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.