如何在10秒后停止此计时器?

时间:2014-03-08 09:09:04

标签: android timer

此代码在按钮点击时在Android屏幕上运行计时器。但我想在10秒停止此计时器。我想我们可以在thread秒访问'secs'变量值并终止10。但我不知道该怎么做。还有其他想法吗?那么如何在10秒停止时间?

package com.example.image_changer;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView textView;
ImageView imageView;
ImageView imageView1;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMillisecond = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     button = (Button)findViewById(R.id.button1);
     textView = (TextView)findViewById(R.id.timerValue);
     imageView = (ImageView)findViewById(R.id.imageView1);
     imageView1 = (ImageView)findViewById(R.id.imageView1);

     button.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
        startTime = SystemClock.uptimeMillis();

        customHandler.postDelayed(updateTimerThread, 0);


        }
    });
}


private Runnable updateTimerThread = new Runnable() {

    int secs;

    @Override
    public void run() {
        // TODO Auto-generated method stub
        timeInMillisecond = SystemClock.uptimeMillis()- startTime;
        updatedTime = timeSwapBuff+timeInMillisecond;
        secs = (int)(updatedTime/1000);
        int mins = secs/60;
        secs = secs % 60;
        int milliseconds = (int)(updatedTime % 10000);
        textView.setText("" + mins + ":" + String.format("%02d", secs)+
                ":"+ String.format("%03d", milliseconds));

        customHandler.postDelayed(this,0);



    }

};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

我试试这些解决方案:

@Override
        public void onClick(View arg0) {


            // TODO Auto-generated method stub
        startTime = SystemClock.uptimeMillis();

        customHandler.postDelayed(updateTimerThread, 0);

并在线程中

int milliseconds = (int)(updatedTime % 10000);
        textView.setText("" + mins + ":" + String.format("%02d", secs)+
                ":"+ String.format("%03d", milliseconds));

        customHandler.postDelayed(this,1000);

然后它在第二个(部分)

中计数1到无限制

第二个解决方案尝试

如果我在线程调用中尝试此操作,那么它就不会开始计数。

@Override
        public void onClick(View arg0) {


            // TODO Auto-generated method stub
        startTime = SystemClock.uptimeMillis();

        customHandler.postDelayed(updateTimerThread, 0);
        customHandler.removeCallbacks(updateTimerThread);

我想要什么 - 计时器手表运行但是它在10秒内停止(仅second部分对我来说很重要)


当我尝试这个时,它在第二部分从10开始计数到无限数。

       @Override
        public void onClick(View arg0) {


            // TODO Auto-generated method stub
        startTime = SystemClock.uptimeMillis();

        customHandler.postDelayed(updateTimerThread, 10000);
        //customHandler.removeCallbacks(updateTimerThread);

在线程中

     timeInMillisecond = SystemClock.uptimeMillis()- startTime;
        updatedTime = timeSwapBuff+timeInMillisecond;
        secs = (int)(updatedTime/1000);
        int mins = secs/60;
        secs = secs % 60;
        int milliseconds = (int)(updatedTime % 10000);
        textView.setText("" + mins + ":" + String.format("%02d", secs)+
                ":"+ String.format("%03d", milliseconds));

        customHandler.postDelayed(this,0);

2 个答案:

答案 0 :(得分:2)

单击按钮开始倒计时,完成时删除处理程序回调..

     new CountDownTimer(10000, 1000) {

 public void onTick(long millisUntilFinished) {

 }

 public void onFinish() {
    customHandler.removeCallbacks(updateTimerThread);
 }
}.start();

答案 1 :(得分:0)

您可以使用倒计时器 10秒。它很容易实现。您可以check this project获取帮助。