java - 检查是否运行倒计时?

标签 java android timer countdowntimer

我有定时器,我可以将定时器设置为响铃。 我还有一个暂停按钮来停止计时器。 当您第一次访问应用程序时,会导致没有计时器暂停。 当您单击暂停按钮时,我收到错误。 这是我的计时器:

long result = date2.getTime() - date1.getTime();

                        timer = new CountDownTimer(result, 1000) {


                            public void onTick(long millisUntilFinished) {
                                time.setText(("seconds remaining: " + millisUntilFinished / 1000));
                                //create HashMap<Integer,String> textMap at the constructer of the adapter
                                //now fill this info int'o it
                                textMap.put(new Integer(position), "seconds remaining: " + millisUntilFinished / 1000);
                                //notify about the data change
                                notifyDataSetChanged();

                            }

                            public void onFinish() {

                                time.setVisibility(View.INVISIBLE);
                                //create HashMap<Integer,String> textMap at the constructer of the adapter
                                //now fill this info into it

                                textMap.put(new Integer(position),null);
                                //notify about the data change
                                notifyDataSetChanged();
                                Toast.makeText(getApplicationContext(), "finish", Toast.LENGTH_LONG).show();
                                if (rowView != lastview || mediaPlayer == null) {
                                    play(position);
                                    if (lastview != null)
                                        lastview = rowView;
                                } else {
                                    play.setImageResource(R.drawable.n_btn_play_unselected);
                                    mediaPlayer.release();
                                    lastview = null;
                                }


                            }

                        }.start();
                        deleteDialog.dismiss();
                    }
                });

这是我的暂停按钮:

      stop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                time.setVisibility(View.INVISIBLE);
                //create HashMap<Integer,String> textMap at the constructer of the adapter
                //now fill this info into it
              int  position=0;
                textMap.put(new Integer(position),null);
                //notify about the data change
                notifyDataSetChanged();

                    timer.cancel();


            }            });

如何检查我的时间是否正在运行? 点击它时我得到了 nullpointexecption。

最佳答案

检查它是否正在运行:

if(timer != null){
    //Here the timer will be running
}

但是,当您取消时,您必须执行以下操作:

timer.cancel();
timer = null;

因此,如果计时器不为空,则表示它正在运行。如果为空则表示用户已取消。

关于java - 检查是否运行倒计时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37532064/

相关文章:

java - 如何序列化 ByteBuffer

java - 信任库中的 "Anchor for certification path not found"

android - 将相机捕获的图像存储在 SQLite 数据库中

java - 尝试在对话框中使 ImageView 全尺寸不起作用

jquery 水平时间栏在可变秒数后提交页面

java - 使用计时器绘制在 JFrame 上滚动的图形

java - JPA 持久性属性干扰 JAXB

java - 使 JComponent 不透明

android - Android Studio 中的 ARToolkit

ios - 在后台运行定时器