我正在构建一个Android应用程序,计算自其中一个活动开始以来的时间,第一次启动活动一切正常,计时器工作正常,它在TextView上显示时间。 当我完成活动(翻转手机),并尝试重新启动时,计时器工作正常(我将其显示为日志)但文本视图不会更改
override fun doInBackground(vararg params: Activity): String? {
val acc = Accelerometer(params[0])
dealsList.forEach {Log.d("fallDeals", it) }
while (true) {
sleep(100)
publishProgress()
Log.i("TAG", "${tiltSensorsValues[0] - acc.xPos!!} ${tiltSensorsValues[1] - acc.yPos!!} ${acc.zPos!!}")
if (flippedPhone(acc)) {
Log.i("REFRENCES", "${dbRefrences.stateRef}")
dbRefrences.stateRef.setValue("empty")
addToOldDataTableDatabase()
return "Triggered"
break
}
}
}
private fun flippedPhone(acc: Accelerometer) =
(tiltSensorsValues[0] - acc.xPos!! < -4 || tiltSensorsValues[0] - acc.xPos!! > 4 ||
tiltSensorsValues[1] - acc.yPos!! < -4 || tiltSensorsValues[1] - acc.yPos!! > 4)
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
dbRefrences.currentRef.child(USERS).removeValue()
dbRefrences.stateRef.setValue("empty")
Toast.makeText(context,setTimer(),Toast.LENGTH_SHORT).show()
}
//*********************************************
//Functions
//*********************************************
/**
* adding the data to the old data table
*/
private fun addToOldDataTableDatabase() {
val oldDataTableRef = dbRefrences.tableRef.child(OLD_DATA)
oldDataTableRef.child(gDate()).child(getCurrentTime())?.child(TIME)?.setValue(timer.text)
Log.i("My Service", userList.size.toString())
userList.forEach {
oldDataTableRef.child(gDate()).child(getCurrentTime())?.child(PARTICIPANTS)?.
child(it.userId)?.setValue(it.name)
}
}
private fun gDate(): String {
val c = Calendar.getInstance()
val df = SimpleDateFormat("dd-MM-yyyy")
return df.format(c.time)
}
/**
* return the time that has passed since the group started
*/
private fun setTimer(): String {
val difference = Calendar.getInstance().timeInMillis - startTime
val minutes = TimeUnit.MILLISECONDS.toMinutes(difference).rem(60)
val seconds = TimeUnit.MILLISECONDS.toSeconds(difference).rem(60)
val hours = TimeUnit.MILLISECONDS.toHours(difference).rem(24)
Log.i("TAG", "${String.format("%02d", hours)}:${String.format("%02d", minutes)}:${String.format("%02d", seconds)}")
return "${String.format("%02d", hours)}:${String.format("%02d", minutes)}:${String.format("%02d", seconds)}"
}
/**
* changing the textView text to the time that has passed since they started
*/
override fun onProgressUpdate(vararg values: Void?) {
super.onProgressUpdate(*values)
timer.text = setTimer()
}
顺便说一句,我正在使用kotlin