我已經實作了一個在后臺運行的 NotificationListenerService。
通過這個服務 onNotificationPosted 方法,我希望能夠在 2 分鐘內排隊作業。
解決此問題的最佳方法是什么?
JobScheduler 和 Workmanager 的最小間隔為 15 分鐘。因為我已經運行了后臺服務,所以我認為我真的不需要使用它們,對嗎?
我可以在我的 onNotificationPosted 方法中使用 TimerTask 之類的東西,甚至是 sleep 嗎?
uj5u.com熱心網友回復:
WorkManager
has 15 minutes interval only for periodic tasks, thats is not what you want, as I can see. You can schedule task with OneTimeWorkRequest
to let it be executed in 2 minutes after the onNotificationPosted
method call.
由于您已經擁有運行服務,您可能只能在單獨的Coroutine范圍中延遲任務
launch {
delay(2 * 60 * 1000)
yourTask()
}
或使用
handler.postDelayed(2 * 60 * 1000) {
yourTask()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/506573.html
標籤:安卓 android-workmanager 定时器任务 Android-Jobscheduler
上一篇:在java中訪問內部類
下一篇:RTDB查詢會減少帶寬嗎?