【问题标题】:How to use timeLeft for resume countdowntimer on Android?如何在 Android 上使用 timeLeft 进行恢复倒计时?
【发布时间】:2017-05-13 06:54:32
【问题描述】:

timeLeft 正在播放音乐文件,但如何使用 onResume? 所以我可以暂停活动,然后用左计时器继续。

这是我的代码:

public void countdownTimer () {

    final TextView mTextField = (TextView)findViewById(R.id.timer);
    Count = new CountDownTimer(TIMER, 1000) {
        public void onTick(long millisUntilFinished) {

             mTextField.setText("" +(millisUntilFinished / 60000));
             long timeLeft = millisUntilFinished / 1000;
             if(timeLeft <= 4 && timeLeft >=2 && tgbutton.isChecked())
             {
                 mSoundPool.play(sixthMusicFile, 1f, 1f, 1, 0, 1f);
             }
             if(timeLeft <= 1 && tgbutton.isChecked())
             {
                mSoundPool.play(seventhMusicFile, 1f, 1f, 1, 0, 1f);
                vibrate(); 
             }

         }
         public void onFinish() {
             Count.setText("done!");
         }
      }.start();
}

已编辑: 最后,我得到了一些答案。这里的工作代码: Android CountDown Timer with Pause, Resume and Cancel button。 非常感谢之前帮助过我的所有人。

【问题讨论】:

  • 你需要的在这里:stackoverflow.com/a/10055480/5552022
  • 感谢@omar-aflak 的回复。但让我很困惑。
  • 基本上你需要的是一个 CountDownTimer,你可以随时暂停/恢复,不是吗?
  • 是的。我会先试试你的建议。谢谢。
  • 你也可以参考这个答案:stackoverflow.com/a/5738427/5552022这几乎就是@vivek-mahajan所做的

标签: android countdowntimer resume pause


【解决方案1】:

试试这个活动:

    public class MyActivity extends AppCompatActivity {

                    long TIMER = 1000000;
                    long timeLeft = 0;
                    private CountDownTimer Count;
                    SharedPreferences sharedPreferences;

                    @Override
                    protected void onCreate(@Nullable Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        sharedPreferences = getSharedPreferences("App_shared_preferenced", Context.MODE_PRIVATE);

                    }

                    @Override
                    protected void onResume() {
                        super.onResume();
                        timeLeft=  sharedPreferences.getLong("leftTime",0);
                        if(timeLeft>0)
                        countdownTimer(timeLeft);
                        else countdownTimer(TIMER);
                    }

                    @Override
                    protected void onPause() {
                        super.onPause();
                        sharedPreferences.edit().putLong("leftTime",timeLeft).commit();
                        if(Count != null){                                                                
                          Count.cancel();
                        }
                    }

                    public void countdownTimer(long t) {
                          if(Count != null) Count.cancel();
                        final TextView mTextField = null;// Your text view
                        Count = new CountDownTimer(t, 1000) {
                            public void onTick(long millisUntilFinished) {

                                mTextField.setText("" + (millisUntilFinished / 60000));
                                timeLeft = millisUntilFinished / 1000;
                                if (timeLeft <= 4 && timeLeft >= 2 && tgbutton.isChecked()) {
                                    mSoundPool.play(sixthMusicFile, 1f, 1f, 1, 0, 1f);
                                }
                                if (timeLeft <= 1 && tgbutton.isChecked()) {
                                    mSoundPool.play(seventhMusicFile, 1f, 1f, 1, 0, 1f);
                                    vibrate();
                                }
                            }

                            public void onFinish() {
                                Count.setText("done!");
                                timeLeft = 0;
                            }
                        }.start();
                    }
                }

【讨论】:

  • 我认为你的代码很好。但是,当我暂停时,记录说 timeLeft = 0。当我回到应用程序时,记录说 timeLeft = 0。我不知道这是怎么回事。
  • 如果用户从后台删除应用程序或在活动失真时终止应用程序,您希望它这样做吗?如果是,那么我已经更新了我的答案代码。
  • 当用户按下“主页按钮”时停止倒计时。当应用程序再次播放时,计时器以 timeLeft 开始。现在,就像 2 次倒计时在不同的时间一起运行..
  • onCreate 中的@Nullable 是什么?当我从按下“主页按钮”返回我的应用程序时,仍然显示双重倒计时。顺便说一句,我这样称呼倒计时:public void displayNextQuestion() { countdownTimer(TIMER); }
  • 这是一个注解,它可以为 null 准备编译,它不会受到影响,在 countdownTimer() 方法上创建新的 CountDownTimer 之前添加这些行: if(Count != null){ Count.cancel (); }
猜你喜欢
  • 1970-01-01
  • 2012-01-08
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多