【问题标题】:How can we use the android JobIntentService in the Scala?我们如何在 Scala 中使用 android JobIntentService?
【发布时间】:2021-08-25 10:07:27
【问题描述】:

我可以成功扩展和调用JobIntentService的函数

enqueueWork(context, MessagesRetentionKotlin::class.java, UNIQUE_JOB_ID, work)

在 kotlin 中,但不在 Scala 中。在这里,我将分享我的扩展 JobIntentService 类的 Scala 代码,如果有人可以帮助我,那就太好了。谢谢。

    class MessagesRetention(context: Context, work: Intent) extends  MessagesRetentionTrait with JobIntentService{
 
  override def onCreate():Unit= {
    super.onCreate()
    Log.d(TAG, "onCreate")
  }

 

  override def onHandleWork(intent: Intent):Unit=  {
    Log.d(TAG, "onHandleWork")
    val input: String = intent.getStringExtra("inputExtra")
    for (i <- 0 to 10) {
      Log.d(TAG, "$input - $i")
      if (isStopped) return
      SystemClock.sleep(1000)
    }
  }

  override def onDestroy():Unit=  {
    super.onDestroy()
    Log.d(TAG, "onDestroy")
    val serviceIntent =  new Intent(this, MessagesRetentionImpl.getClass)
    serviceIntent.putExtra("inputExtra", "Test")
    MessagesRetention.enqueueWorkk(this, serviceIntent)
  }
}

object MessagesRetention {
  private val TAG = "MessagesRetention"
  val UNIQUE_JOB_ID = 10101
  def  enqueueWorkk(context: Context, work: Intent):Unit=  {
     enqueueWork(context, MessagesRetentionImpl.getClass, UNIQUE_JOB_ID, work)
  }
}

【问题讨论】:

    标签: android scala kotlin service scala-collections


    【解决方案1】:

    作为 scala 的新手,我没有将正确的服务上下文传递给 JobIntentService 的 enqueueWork 方法。我没有使用.getclass 方法,而是使用classOf[T] 进行服务类传递并且它有效。

    enqueueWork(context, classOf[MessagesRetention], UNIQUE_JOB_ID, work)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      相关资源
      最近更新 更多