我在尝试用一个停止按钮停止我的两个可运行程序时遇到问题,我可以只停止两个中的一个就可以了,但是一旦我尝试停止我的应用程序在按下手机上的按钮时冻结,这里是到目前为止我的代码:
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();
}
});
}
}
任何帮助将不胜感激!谢谢。