简体   繁体   English

如何使用多线程在 android 工作室中不断检查时间

[英]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.事实证明,一旦用户登录应用程序,我就有一个想要的项目,开始不断获取日期和时间 (hh:mm:ss) 并将这些数据连续传递给另一个片段。 All this as long as the user can perform other actions in the app, can someone help me with this?所有这一切只要用户可以在应用程序中执行其他操作,有人可以帮助我吗? i used API 32我用 API 32

Based on the comments, I think what you need is to calculate the time passed by using System.getTimeInMillis() .根据评论,我认为您需要计算使用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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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