如何让计时器在后台运行

时间:2012-08-07 23:13:17

标签: java android timer

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

    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()中声明,谢谢!

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

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

否则,如果计时器一直打勾的意图将其转换为意向服务。

答案 2 :(得分: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)
       }