title: How to start a foreground service from a respository
body: <p>I have a single activity Kotlin-based app that starts a foreground service with:</p>
  <pre><code>applicationContext.startForegroundService(serviceIntent)
  </code></pre>
  <p>Next I bind to the service so that I can obtain an Ibinder object and communicate with it via messages (in both directions) using:</p>
  <pre><code>bindService(serviceIntent, myConnection, Context.BIND_IMPORTANT)
  </code></pre>
  <p>In the service's onCreate method I execute</p>
  <pre><code>startForeground(1, notification) // posts a persistent notification
  </code></pre>
  <p>So that the service will keep running even when the user is running another app. All this is working well.</p>
  <p><strong>Now I want to refactor the app to a single activity that hosts multiple fragments</strong>. I'd like to share the service between the fragments (several of them anyway). (I've got the fragments navigating cleanly.)</p>
  <p>I'd like to follow the MVVM design pattern and put the start/stop service control in a repository class (as suggested by <a href="https://stackoverflow.com/questions/47265685/what-is-the-right-place-to-start-a-service-in-mvvm-architecture-android">What is the right place to start a service in an MVVM architecture</a> on Stackoverflow.</p>
  <p>However, I can't seem to get it to work. I've tried the following (among other things!):</p>
  <pre><code>  import android.content.Intent
    import androidx.core.content.ContextCompat.startForegroundService
  
    class myRepository {
  
      fun initService() {
  
          val serviceIntent = Intent(this, MyService::class.java)
          startForegroundService(serviceIntent)
   
      }
    }
  </code></pre>
  <p>The first line in the function has &quot;Intent&quot; underlined in red with the message &quot;None of the following functions can be called with the arguments supplied.&quot;</p>
  <p>Also the closing parenthesis of the startForegroundService statement has a red underline that when hovered over says &quot;No value passed for paramenter 'intent'.&quot; So something seems to be wrong with my Intent statement.</p>
  <p>Is there something simple (hopefully) that I'm just missing? Does it have to do with the context of my repository?</p>