Android计时器无法取消

时间:2013-06-17 08:23:24

标签: android

我正在开展测验申请。我需要保留一个计时器,如果单击暂停按钮按钮,计时器应该停止,如果再次单击播放按钮,计时器应该从之前停止的位置开始。   现在我的问题是当我点击暂停按钮时,计时器没有停止。请问有人可以查看这个问题吗?真的很感激......

My Code:



public void onCreate(Bundle savedInstanceState) {

    timervalue = 3 * 80000

    btn = (ImageView) findViewById(R.id.imageView2);
            btn.setImageResource(R.drawable.pause);
            btn.setVisibility(View.VISIBLE);
        }

    LongOperation1 op = new LongOperation1();
                op.execute("");
    }


    private class LongOperation1 extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            loadFeed2();
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            dialog1.dismiss();
            try {

                if (modeprefName.equals("timed")) {

                        counter = new MyCount(timervalue, 1000);
                        counter.start();
            }
            setquestion();
    }

catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        @Override
        protected void onPreExecute() {
            dialog1 = ProgressDialog.show(Taketest.this, "Please wait...",
                    "Retrieving data ...", true);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }


@Override
public void onClick(View v) {
    if (b == false) {
        b = true;

        counter.cancel();
        btn.setImageResource(R.drawable.play);
    } else {
        b = false;

        counter = new MyCount(s1, 1000);
        counter.start();
        btn.setImageResource(R.drawable.pause);
    }

}

public class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        time.setText("DONE");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        s1 = millisUntilFinished;

        timervalue = s1;
        ss1 = millisUntilFinished / 1000;

        String seconds = String
                .valueOf((millisUntilFinished / (1000)) % 60);
        String minutes = String.valueOf((millisUntilFinished / 60000) % 60);
        String hours = String.valueOf(millisUntilFinished / 3600000);
        if (seconds.length() > 1) {

            // time.setText("Time Left: "+ minutes+":"+seconds);
        } else if (seconds.length() <= 1) {
            seconds = "0" + seconds;
        }

        if (minutes.length() > 1) {

        } else if (minutes.length() <= 1) {
            minutes = "0" + minutes;
        }
        if (hours.length() > 1) {

        } else if (hours.length() <= 1) {
            hours = "0" + hours;
        }
        time.setText("Time: " + hours + ":" + minutes + ":" + seconds);

    }
}

0 个答案:

没有答案