我想停止onDestroy()中的处理程序。代码如下。blink()方法在活动中出于特定原因调用,但希望在销毁方法中停止它们的服务。
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();
}发布于 2019-03-29 15:13:39
treadRunning布尔值onDestroy()方法的句柄设置treadRunning = 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();
}
}https://stackoverflow.com/questions/55371040
复制相似问题