我想在onDestroy()中停止处理程序。代码如下。 blink()方法由于活动中的特定原因而调用,但希望将其服务停止在destroy方法中。
final Handler handler = new Handler();
private void blink() {
PrintLog.log("On", "Blink Thread");
new Thread(new Runnable() {
@Override
public void run() {
int timeToBlink = 1000; //in milissegunds
try {
Thread.sleep(timeToBlink);
} catch (Exception e) {
}
handler.post(new Runnable() {
@Override
public void run() {
if (text_ATMCardInstruction.getVisibility() == View.VISIBLE) {
text_ATMCardInstruction.setVisibility(View.INVISIBLE);
} else {
text_ATMCardInstruction.setVisibility(View.VISIBLE);
}
blink();
}
});
}
}).start();
}
@Override
protected void onDestroy() {
// what is code here?
PrintLog.log("Stop", "serviceStop");
super.onDestroy();
}
答案 0 :(得分:0)
treadRunning布尔值onDestroy()方法上的句柄设置footRunning = false;
private void blink() {
PrintLog.log("On", "Blink Thread");
if (treadRunning) {
new Thread(new Runnable() {
@Override
public void run() {
int timeToBlink = 1000; //in milissegunds
try {
Thread.sleep(timeToBlink);
} catch (Exception e) {
}
handler.post(new Runnable() {
@Override
public void run() {
if (text_ATMCardInstruction.getVisibility() == View.VISIBLE) {
text_ATMCardInstruction.setVisibility(View.INVISIBLE);
} else {
text_ATMCardInstruction.setVisibility(View.VISIBLE);
}
blink();
}
});
}
}).start();
} else {
PrintLog.log("On", "Blink Thread Stop");
new Thread(new Runnable() {
@Override
public void run() {
text_ATMCardInstruction.setVisibility(View.INVISIBLE);
}
}).interrupt();
}
}