第二次启动AsyncTask

我正在建立一个Android应用程序,从一个活动开始的时候开始计算时间,第一次我开始活动一切正常,计时器工作正常,它显示在TextView上的时间。 当我完成活动(翻转手机),并尝试再次启动计时器工作正常(我显示为一个日志),但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[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

简单的定时器应用程序来解决你的问题可以这样实现:

 class TimerApp : Application() {//TODO add TimerApp to manifest val startTime: Long by lazy { System.currentTimeMillis() } companion object { lateinit var instance: TimerApp private set } override fun onCreate() { super.onCreate() instance = this } } class FirstActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) //init your views val timerTV = TextView(this) timerTV.postDelayed({ timerTV.text = TimerApp.instance.startTime.getElapsed() }, 1000) } } fun Long.getElapsed(): String { val delta = System.currentTimeMillis() - this //elapsed time in ms TODO("return formatted string") }