0

我在尝试用一个停止按钮停止我的两个可运行程序时遇到问题,我可以只停止两个中的一个就可以了,但是一旦我尝试停止我的应用程序在按下手机上的按钮时冻结,这里是到目前为止我的代码:

    if(go != 1){
        go = 1;
    final Timer t =new Timer();
    t.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                public void run() {
                    if(DHPDPS==0){
                        money = (DPPS+Reserve);
                        Reserve = (money);
                        String end = String.format("%1f", money);
                        t1.setText("$" + end);
                    }else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){
                        money = (DOTPPS+Reserve);
                        Reserve = (money);
                        String end = String.format("%1f", money);
                        t1.setText("$" + end);
                    } else{

                        money = (DPPS+Reserve);
                        Reserve = (money);
                        String end = String.format("%1f", money);
                        t1.setText("$" + end);
                    }
                    //Place your stopping condition over here. Its important to have a stopping condition or it will go in an infinite loop. 
                    counter++;

                    // Display pay per second
                    if(counter <= DHPDPS || DHPDPS == 0){
                    t2.setText("Your pay per second is: $"+result);
                    }else{
                        t2.setText("Your pay per second is: $"+result2);
                    }
                }
            }); 
        }
    }, 20, 20);

 // Make countdown to overtime display
    final TextView count = (TextView) findViewById(R.id.countdown);
    countdown = (int)HPDPS;
    cd.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run(){
                    int hours = (countdown/3600);
                    if(OTPPS != 0 && HPDPS != 0){
                            count.setText("Seconds Remaining to Overtime: " + countdown + "\nAbout " + hours + " Hours");
                            countdown--;
                        }
                }
            });
        }

    }, 1000, 1000);



    final Button b = (Button) findViewById(R.id.clockout);
    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if(go == 1)
                go = 0;
            if (t != null)
                t.cancel();
            // if (cd != null)  // This condition Freezes my phone when activated?
            //  cd.cancel();
            }
    });

    } 

}

任何帮助将不胜感激!谢谢。

4

3 回答 3

1
public void run() {
    while(running) {
         //your code for both runnables
    }
}

final Button b = (Button) findViewById(R.id.clockout);
b.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if(running) {
            running = false;
        }    
    }
});

使用此代码,您的可运行对象将检查它们是否应该运行每个循环,如果不是,它们将退出它们run()并且线程将停止。

于 2012-08-03T12:05:21.900 回答
1

我已经弄清楚问题出在哪里,结果证明是一个非常简单的修复,这是我的原始代码字节:

    final Button b = (Button) findViewById(R.id.clockout);
b.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if(go == 1)
            go = 0;
        if (t != null)
            t.cancel();
        // if (cd != null)  // This condition Freezes my phone when activated?
        //  cd.cancel();
        }

这是我为解决问题所做的:

    final Button b = (Button) findViewById(R.id.clockout);
    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if(go == 1)
                go = 0;
            if (t != null){
                t.cancel();
                cd.cancel();
            }
            }

这非常简单,我猜只是我看过的东西,谢谢大家的帮助!

于 2012-08-03T13:39:00.910 回答
0

1.创建一个boolean变量,该变量将true在开始时设置为...

2.在两者中都使用这个变量runnables...while (isOk)

3.点击按钮,将此boolean变量设为false两个runnnable都将不存在...

于 2012-08-03T12:45:45.083 回答