Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Execute function every full minute android/kotlin

Posted on November 11, 2022 By

Solution 1 :

You have to first calculate the time required to become exact minute that is starting the task at 8:30:12, first call should happen at 8:31:00.

So for that you can pull current time using System.currentTimeMillis() and then ceil it’s minute value to the nearest integer,

// import kotlin.math.ceil
val firstCallTime = ceil(System.currentTimeMillis() / 60_000.0).toLong() * 60_000

Then you can use coroutines to handle your task that will use a CommonPool to get reusable threads from and won’t require to you to create threads.

// scope can be anything, for instance you want to call the function
// only when client is using your application then use viewModelScope provided,
// otherwise your own scope like `val scope = CoroutineScope(Dispatchers.Main)`
// Or use Dispatchers.Default for CPU intensive tasks

val parentJob = scope.launch {
    // suspend till first minute comes after some seconds
    delay(firstCallTime - System.currentTimeMillis())
    while (true) {
        launch {
            yourFunctionToBeCalled()
        }
        delay(60_000)  // 1 minute delay (suspending)
    }
}

// If you ever want your job to cancel just do a cancel,
// the function delay suspends and so cancellable without interrupting the threads
// parentJob.cancel()

Solution 2 :

Use PeriodicWorkRequest along with WorkManager class in android. It goes like this:

    val work = PeriodicWorkRequestBuilder<MyWorker>(60, TimeUnit.SECONDS)
.build();
    val workManager = WorkManager.getInstance(context);
    workManager.enqueuePeriodicWork(work);

Problem :

Is this possible to execute function in android every minute starting from full minute, so eg. when I run my app at 8:30:12 the first function call will be at 8:31:00 and next at 8:32:00 and so on.

Comments

Comment posted by Le Nguyen Duy Anh

WorkManager

READ  [FIXED] java - Date to local time kotlin
Powered by Inline Related Posts
Android Tags:android, kotlin

Post navigation

Previous Post: [FIXED] java – How can I solve this Android Studio Firebase problem?
Next Post: [FIXED] Android Java Publish result cast issue at MyCustomAdapter.java

Related Posts

[FIXED] java – Android rx call multiples sentences in sequence Android
[FIXED] android – onActivityForResult not returning any data for Intent Android
[FIXED] kotlin – Android data class unit test Android
[FIXED] java – Android Studio SQLITE Database is read only if I add drop database function in sqlhelper class Android
[FIXED] android – Shared element transition is not working, if app open through deep link Android
[FIXED] android – How to generate and publish Code Coverage in gradle kotlin using Azure DevOps? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme