Schedule Task To Be Executed Later (time + Date) Android
I just search for code that let me do an action after the timer (with specific date and time) finish (in Kotlin) and save it in a list Like timer for post a tweet on Twitter: https
Solution 1:
You can use WorkManager for that.
Dependency:
implementation "androidx.work:work-runtime-ktx:2.3.0"
Example:
classLogWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
overridefundoWork(): Result {
// Do the work here--in this case, upload the images.
Log.i("ToastWorker", "doWork: Working ⚒ ⚒ ⚒")
// Indicate whether the task finished successfully with the Resultreturn Result.success()
}
}
Then set the delay time
vallogWorkRequest= OneTimeWorkRequestBuilder<LogWorker>()
.setInitialDelay(5, TimeUnit.SECONDS) // here you can set the delay time in Minutes, Hours
.build()
Start the timer
WorkManager.getInstance(this).enqueue(toastWorkRequest)
Here is a Codelab for more insight. You can also read more here
Post a Comment for "Schedule Task To Be Executed Later (time + Date) Android"