我有一个从按钮单击侦听器启动的异步作业。这是一项打印工作,因此它会在一天中一次又一次地发生。我想确保在打印作业之间我没有泄漏线程或对象。我只需要 looper.mylooper.quit() 行吗?
监听器代码是:
private final Button.OnClickListener cmdPrintOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
enableScreenForUse(false);
setStatus("Saving any changes");
saveButtonClicked();
TwoInchZebraDocket newDocket = new TwoInchZebraDocket(mRunDetailID, getContentResolver()); //could set this to final if we get problems -> see for details http://stackoverflow.com/questions/11424924/how-can-i-pass-an-object-to-a-new-thread-generated-anonymously-in-a-button-liste/11425012?iemail=1#11425012
setStatus("Creating docket for printing");
PrintRunnable target = new PrintRunnable(newDocket, CheckoutActivity.this);
new Thread(target).start();
}
};
可运行的目标代码是:
public void run() {
Looper.prepare();
printDocket();
Looper.loop();
Looper.myLooper().quit();
}