mauricio romero
mauricio romero

Reputation: 47

how to check the time constantly in android studio with multithreading

I'm new to the issue of parallel processes and therefore I have some doubts. It turns out that I have a project which I want once the user logs in to the app, start to constantly get the date and time (hh:mm:ss) and pass this data continuously to another fragment. All this as long as the user can perform other actions in the app, can someone help me with this? i used API 32

Upvotes: 0

Views: 221

Answers (1)

ndriqa
ndriqa

Reputation: 401

Based on the comments, I think what you need is to calculate the time passed by using System.getTimeInMillis(). E.g.

// you save the last time user did something he/she can do
// I'm using sharedPreferences as an example, you can save it in any way you want
// depending on the scope you need
val lastTimeUserDidSmthInMillis = sharedPreferences(LAST_TIME_OF_ACTION, 0L)

// current time given by the system
val currentTimeInMillis = System.getTimeInMillis()

// time difference in seconds
val timePassedInMillis = (currentTimeInMillis - lastTimeUserDidSmthInMillis) / 1000

if (timePassedInMillis > TIME_THRESHOLD) {
  doSomething()
} else {
  // not enough time has passed yet
}

Upvotes: 2

Related Questions