1

当应用程序停止时,我将如何有效地让计时器在后台运行?现在仪式我没有做任何事情,当应用程序停止时计时器将继续运行,但有时它会在没有我给它命令的情况下停止运行。这就是我目前运行计时器的方式:

    if(t == null){
   t = new Timer();
    t.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                public void run() {
                    if(DHPDPS==0){
                        money = (DPPS+Reserve);
                        Reserve = (money);
                        String end = String.format("%1f", money);
                        t1.setText("$" + end);
                    }else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){
                        money = (DOTPPS+Reserve);
                        Reserve = (money);
                        String end = String.format("%1f", money);
                        t1.setText("$" + end);
                    } else{

                        money = (DPPS+Reserve);
                        Reserve = (money);
                        String end = String.format("%1f", money);
                        t1.setText("$" + end);
                    }
                    counter++;
                    //if(counter == 3000)
                    //   t.cancel();

                    // Display pay per second
                    if(counter <= DHPDPS || DHPDPS == 0){
                    t2.setText("Your pay per second is: $"+result);
                    }else{
                        t2.setText("Your pay per second is: $"+result2);
                    }
                }
            }); 
        }
    }, 20, 20);

它在 onCreate() 中声明,谢谢!

4

3 回答 3

3

对于在后台运行的任务,您应该使用IntentService。即使您的活动被操作系统暂停或删除,它也会继续运行。

于 2012-08-07T23:17:17.953 回答
0

如果您的手机内存开始填满,它将关闭所有未主动使用的活动。

对于像计时器这样的东西,最好将开始时间保存到 SQLite 中,并在重新加载活动时将其作为计算持续时间的起点。

否则,如果您的意图是计时器不断滴答作响,则将其转换为意图服务。

于 2012-08-07T23:20:18.333 回答
0

private fun startTimer() 
       var lastdata = PrefUtils.getpunchtime(this)
       var mEndTimediff = System.currentTimeMillis() - lastdata!!
       var mTimeLeftInMillis = viewModel.START_TIME_IN_MILLIS.toLong() - mEndTimediff

       viewModel.mCountDownTimer = object : CountDownTimer(mTimeLeftInMillis, 1000) {
           override fun onTick(millisUntilFinished: Long) {
               mTimeLeftInMillis = millisUntilFinished
               updateCountDownText(mTimeLeftInMillis)
           }

           override fun onFinish() {
               viewModel.mTimerRunning = false
           }
       }.start()

   }

   private fun updateCountDownText(mTimeLeftInMillis: Long) {

       val millis: Long = mTimeLeftInMillis
       val hms = String.format(
           "%02d : %02d: %02d", TimeUnit.MILLISECONDS.toHours(millis),
           TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(
               TimeUnit.MILLISECONDS.toHours(
                   millis
               )
           ),
           TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(
               TimeUnit.MILLISECONDS.toMinutes(
                   millis
               )
           )
       )

       binding.tvTimertime.setText(hms)
   }

SharePreference in store Last_time


       fun storepunchtime(context: Context, storepunchtime: Long) {
           val editor = getSharedPreferences(context).edit()
           editor.putLong(PUNCH_TIME, storepunchtime)
           editor.apply()
           editor.commit()
       }
       fun getpunchtime(context: Context): Long? {
           return getSharedPreferences(context).getLong(PUNCH_TIME, 0L)
       }
于 2019-11-03T06:12:11.483 回答