如何解决我们如何在 Scala 中使用 android JobIntentService?
我可以成功扩展并调用JobIntentService的函数
enqueueWork(context,MessagesRetentionKotlin::class.java,UNIQUE_JOB_ID,work)
在 kotlin 中,但不在 Scala 中。在这里,我将分享我的 Scala 代码,用于扩展 JobIntentService 的类,如果有人可以帮助我,那就太好了。谢谢。
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,work)
}
}
解决方法
作为 Scala 的新手,我没有将正确的服务上下文传递给 JobIntentService 的 enqueueWork
方法。我没有使用 .getclass
方法,而是使用了 classOf[T]
来传递服务类并且它起作用了。
enqueueWork(context,classOf[MessagesRetention],UNIQUE_JOB_ID,work)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。