我正在开发一个在Android中使用Kotlin的注册表单,在几次失败的尝试之后,我需要阻止表单一段时间,可能是一个小时或30分钟,服务在后台运行,因此应用程序关闭或销毁
在查阅和阅读android文档后,我通过扩展CoroutineWorker()类来解决workmanager的这个问题,但我发现以下问题不允许我继续:
Can't create handler inside thread Thread[DefaultDispatcher-worker-1,5,main] that has not called Looper.prepare()
调用CoroutineWorker的ViewModel
@HiltViewModel
class RegisterViewModel @Inject constructor(
private val registerUseCase: RegisterUseCase
) : ViewModel() {
fun makeApiPost(
context: Context,
card: String,
.... : ...
)
{
viewModelScope.launch {
val retroInstance = RetroInstance.getRetroInstanceAuth(context).create(
RetroService::class.java
)
val call = retroInstance.getGiftCardBalance(
CardBalanceRequest(card, ..., ...)
)
call.enqueue(object : Callback<BalanceResponse> {
override fun onResponse(
call: Call<BalanceResponse>,
response: Response<AppBalanceResponse>
) {
if (response.isSuccessful) {
val destination = response.body()
destination?.let {
_cardBalance.postValue(response.body()!!.payload.data)
}
} else {
val workManager = WorkManager.getInstance(ExtendedApp.myContext)
workManager.enqueue(OneTimeWorkRequest.from(WorkerActivate::class.java))
_cardBalance.postValue(null)
}
}
override fun onFailure(call: Call<CardBalanceResponse>, t: Throwable) {
t.printStackTrace()
_cardBalance.postValue(null)
call.request()
}
})
}
}
}
后台进程
class WorkerActivate(context: Context, workerParameters: WorkerParameters) : CoroutineWorker(
context, workerParameters
)
{
lateinit var countDownTimer: CountDownTimer
override suspend fun doWork(): Result {
val minutesTimer: Long = (60 * 1000 * 30)
countDownTimer = object : CountDownTimer(minutesTimer, 1000){
override fun onTick(millisUntilFinished: Long) {
Log.d("card", "Counting Down: ${millisUntilFinished/1000}")
}
override fun onFinish() {
Log.i("card", "countDownTimer de 15 segundos ok")
}
}
return Result.success()
}
}
1条答案
按热度按时间vh0rcniy1#
在代码中,第13行有问题。