title: Migrating from AsyncTask to Coroutines in Android?
body: <p>I am new to coroutines. This is a general question. I have looked into other questions, but they are more specific to a particular use case.</p>
  
  <p>Currently, I am using the following callback based architecture:</p>
  
  <pre><code>class MyService: Service(), OnComplete {
  ...
  companion object {
     lateinit var serviceContext: Context
  }
  ...
  override fun onCreate() {
     super.onCreate()
     serviceContext = this
  }
  ...
  override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
     MyAsyncTask().execute()     // start the background job
     return super.onStartCommand(intent, flags, startId)
  }
  ...
  override fun onCompleteFunc(resultBundle: Bundle) {
     // do something when the job is finished
  }
  }
  </code></pre>
  
  <p>The AsyncTask is as follows:</p>
  
  <pre><code>class MyAsyncTask: AsyncTask&lt;Any, Any, Any&gt;() {
  ...
  val onComplete by lazy { MyService.serviceContext as OnComplete }
  ...
  override fun doInBackground(vararg params: Any?): Any {
     // do the background job here
     // store the results somewhere
     return 0
  }
  ...
  override fun onPostExecute(result: Any?) {
     super.onPostExecute(result)
     onComplete.onCompleteFunc( // job finished. send the results
        Bundle().apply { 
           // put extras here
        }
     )
  }
  }
  </code></pre>
  
  <p>And the interface:</p>
  
  <pre><code>interface OnComplete {
     fun onCompleteFunc(resultBundle: Bundle)
  }
  </code></pre>
  
  <p>I am very much interested in moving to kotlin coroutines. Kindly help me with this.</p>
  
  <p>Regards,
  Sayantan</p>