简体   繁体   中英

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

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

// 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM