【发布时间】:2020-01-23 22:11:12
【问题描述】:
我只是搜索让我在计时器(具有特定日期和时间)完成(在 Kotlin 中)完成后执行操作并将其保存在列表中的代码
喜欢在 Twitter 上发布推文的计时器: https://business.twitter.com/en/help/campaign-editing-and-optimization/scheduled-tweets.html
【问题讨论】:
标签: android kotlin timer schedule
我只是搜索让我在计时器(具有特定日期和时间)完成(在 Kotlin 中)完成后执行操作并将其保存在列表中的代码
喜欢在 Twitter 上发布推文的计时器: https://business.twitter.com/en/help/campaign-editing-and-optimization/scheduled-tweets.html
【问题讨论】:
标签: android kotlin timer schedule
您可以为此使用WorkManager。
依赖:
implementation "androidx.work:work-runtime-ktx:2.3.0"
示例:
class LogWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
override fun doWork(): Result {
// Do the work here--in this case, upload the images.
Log.i("ToastWorker", "doWork: Working ⚒ ⚒ ⚒")
// Indicate whether the task finished successfully with the Result
return Result.success()
}
}
然后设置延迟时间
val logWorkRequest = OneTimeWorkRequestBuilder<LogWorker>()
.setInitialDelay(5, TimeUnit.SECONDS) // here you can set the delay time in Minutes, Hours
.build()
启动计时器
WorkManager.getInstance(this).enqueue(toastWorkRequest)
这里是Codelab 以获得更多信息。你也可以read more here
【讨论】: