<code><span class="pln"><span style="font-size:12px;"><span style="font-family: arial, helvetica, sans-serif;">public class MyTask extends AsyncTask<Void, Void, Void> { private volatile boolean running = true; private final ProgressDialog progressDialog; public MyTask(Context ctx) { progressDialog = gimmeOne(ctx); progressDialog.setCancelable(true); progressDialog.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // actually could set running = false; right here, but I'll // stick to contract. cancel(true); } }); } @Override protected void onPreExecute() { progressDialog.show(); } @Override protected void onCancelled() { running = false; } @Override protected Void doInBackground(Void... params) { while (running) { // does the hard work } return null; } // ... }</span></span></span></code>
如何停止正在運行的AsyncTask
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.