我正在尝试使用倒数计时器创建一个Android应用程序小部件。我有一个发出HTTP请求的服务,然后是倒数计时器,它更新了app小部件的UI(来自HTTP请求的数据)。无论出于何种原因,倒计时器onTick都不会在服务内部打勾。我没有收到任何错误..
这是我的代码:
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val request = Request.Builder()
.url("URL_TO_CALL")
.build()
//Make API call
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {}
override fun onResponse(call: Call, response: Response) {
if (Looper.myLooper() == null) {
Looper.prepare()
Looper.loop()
}
//Create countdown timer object
var ctd = object : CountDownTimer(countdownTime, 5000) {
override fun onTick(millisUntilFinished: Long) {
//Nothing in here is being called
}
}
Handler().postDelayed({
ctd.start()
}, 500)
}
})
return Service.START_STICKY
}
答案 0 :(得分:0)
您输入了WAKE_LOCK吗?
private PowerManager.WakeLock mWakeLock = null;
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
mWakeLock.acquire();
和onDestroy
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
}
现在,这是当您关闭屏幕时您的应用程序即将关闭