CountDownTimer在主线程上运行

时间:2015-09-17 08:41:40

标签: android timer textview

我实现了一个应用程序,里面有一个启动CountDownTimer的按钮和一个显示剩余时间的TextView。有人告诉我,计时器应该在其他线程而不是主线程上运行,但是当我正常启动计时器时,它在主线程上运行:

_timer = new CountDownTimer(min * 60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tick();
                setChanged();
                notifyObservers();
                clearChanged();
                Log.v("THREAD TIMER : ", "" + Looper.myLooper().getThread().getName());
            }

            @Override
            public void onFinish() {
                end = true;
                setChanged();
                notifyObservers();
                clearChanged();
            }
        };

每次定时器勾选时,我都会使用Observer更新TextView,而更新TextView的代码也会在主线程上运行(我用Looper.myLooper().getThread().getName()确认)。但它根本没有影响TextView,但它确实打印出了我要测试的调试。有人有任何想法解决这个问题吗?

现在这更加困惑了。我提取计时器部分,并形成一个新项目。现在,它奏效了。但当然它仍然没有对我的实际项目起作用:

public class MainActivity extends Activity {
    long TIME = 60000;
    CountDownTimer _timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1 = (Button) findViewById(R.id.button_1);
        Button b2 = (Button) findViewById(R.id.button_2);

        final TextView t1 = (TextView) findViewById(R.id.timer_1);
        TextView t2 = (TextView) findViewById(R.id.timer_2);



        setUpButton1(b1, t1);
        setUpButton2(b2, t2);
    }

    private void setUpButton2(final Button b2, final TextView t2){
        final MyTimer myTimer = new MyTimer();
        Observer ob = new Observer() {
            @Override
            public void update(Observable observable, Object data) {
                //t2.setText(myTimer.toString());
                t2.post(new Runnable() {
                    @Override
                    public void run() {
                        t2.setText(myTimer.toString());
                    }
                });
                Log.v("CLASS TIMER :", Looper.myLooper().getThread().getName() + " thread -- " + myTimer.toString());
            }
        };

        myTimer.addObserver(ob);


        b2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                    if(b2.isPressed()){
                        b2.setPressed(false);
                        myTimer.stopTimer();
                    }else{
                        b2.setPressed(true);
                        myTimer.startTimer();
                    }
                }

                return true;
            }
        });
    }

    private void setUpButton1(final Button b1, final TextView t1){

        b1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP){
                    if (b1.isPressed() == true){
                        b1.setPressed(false);
                        _timer.cancel();
                    } else {
                        b1.setPressed(true);
                        _timer = new CountDownTimer(TIME, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                t1.setText("" + millisUntilFinished/1000);
                                TIME = millisUntilFinished;
                            }

                            @Override
                            public void onFinish() {

                            }
                        };
                        _timer.start();
                    }
                }
                return true;
            }
        });
    }
}

计时器类:

public class MyTimer extends Observable{
    CountDownTimer myTimer;
    long TIME = 60000;

    public MyTimer(){

    }

    public void startTimer(){
        myTimer = new CountDownTimer(TIME, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                TIME = millisUntilFinished;
                setChanged();
                notifyObservers();
                clearChanged();
            }

            @Override
            public void onFinish() {

            }
        };
        myTimer.start();
    }

    public void stopTimer(){
        myTimer.cancel();
    }

    public String toString(){
        return "" + TIME/1000;
    }
}

有人知道这里发生了什么吗?为什么它在这种情况下起作用,但在整个项目中不起作用?

0 个答案:

没有答案