下面列出了android.os.AsyncTask#cancel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void getAddressByGeoCodingLatLng() {
//Get string address by geo coding from lat long
if (mLatitude != 0 && mLongitude != 0) {
if (MapUtility.popupWindow != null && MapUtility.popupWindow.isShowing()) {
MapUtility.hideProgress();
}
Log.d(TAG, "getAddressByGeoCodingLatLng: START");
//Cancel previous tasks and launch this one
for (AsyncTask prevTask : filterTaskList) {
prevTask.cancel(true);
}
filterTaskList.clear();
GetAddressFromLatLng asyncTask = new GetAddressFromLatLng();
filterTaskList.add(asyncTask);
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mLatitude, mLongitude);
}
}
private synchronized void cancel() {
AsyncTask<?, ?, ?> task = inactivityTask;
if (task != null) {
task.cancel(true);
inactivityTask = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
for (AsyncTask task : filterTaskList) {
task.cancel(true);
}
}
private synchronized void cancel() {
AsyncTask<?,?,?> task = inactivityTask;
if (task != null) {
task.cancel(true);
inactivityTask = null;
}
}
private synchronized void cancel() {
AsyncTask<?, ?, ?> task = inactivityTask;
if (task != null) {
task.cancel(true);
inactivityTask = null;
}
}
/**
* 取消计时任务
*/
private synchronized void cancel() {
AsyncTask<?, ?, ?> task = mInactivityTask;
if (task != null) {
task.cancel(true);
// 重置为 null
mInactivityTask = null;
}
}
private synchronized void cancel() {
AsyncTask<?,?,?> task = inactivityTask;
if (task != null) {
task.cancel(true);
inactivityTask = null;
}
}
private synchronized void cancel() {
AsyncTask<?,?,?> task = inactivityTask;
if (task != null) {
task.cancel(true);
inactivityTask = null;
}
}
/**
* Call cancel on all tasks and then wipe the living task list.
*/
public static void cancelTasks() {
synchronized (livingTasks) {
for (AsyncTask task : livingTasks) {
task.cancel(true);
}
livingTasks.clear();
}
}
@Override
protected void onPause() {
AsyncTask<?,?,?> oldTask = networkTask;
if (oldTask != null) {
oldTask.cancel(true);
networkTask = null;
}
super.onPause();
}
@Override
protected void onPause() {
AsyncTask<?,?,?> task = backgroundTask;
if (task != null) {
task.cancel(true);
backgroundTask = null;
}
super.onPause();
}
private void launchSearch() {
String query = queryTextView.getText().toString();
if (query != null && !query.isEmpty()) {
AsyncTask<?,?,?> oldTask = networkTask;
if (oldTask != null) {
oldTask.cancel(true);
}
networkTask = new NetworkTask();
networkTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, query, isbn);
headerView.setText(R.string.msg_sbc_searching_book);
resultListView.setAdapter(null);
queryTextView.setEnabled(false);
queryButton.setEnabled(false);
}
}
void cancelTasks() {
for (AsyncTask task : taskList) {
task.cancel(true);
}
taskList.clear();
}
/**
* 如果任务未执行完毕,则取消任务
*
* @param clazz Activity Class
*/
public void unregisterAsyncTask(Class<? extends BaseActivity> clazz) {
ArrayList<AsyncTask> tasks = new ArrayList<>(mTasks.removeAll(clazz));
int size = tasks.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
AsyncTask task = tasks.get(i);
//you may call the cancel() method but if it is not handled in doInBackground() method
if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) {
task.cancel(true);
}
}
}
}
@Override
protected void onPause() {
AsyncTask<?,?,?> oldTask = networkTask;
if (oldTask != null) {
oldTask.cancel(true);
networkTask = null;
}
super.onPause();
}
@Override
protected void onPause() {
AsyncTask<?,?,?> oldTask = networkTask;
if (oldTask != null) {
oldTask.cancel(true);
networkTask = null;
}
super.onPause();
}
@Override
protected void onPause() {
AsyncTask<?,?,?> task = backgroundTask;
if (task != null) {
task.cancel(true);
backgroundTask = null;
}
super.onPause();
}
private synchronized void cancel() {
AsyncTask<?, ?, ?> task = inactivityTask;
if (task != null) {
task.cancel(true);
inactivityTask = null;
}
}
@CallSuper
@Override
public void destroy() {
for (AsyncTask asyncTask : asyncTasks) {
asyncTask.cancel(true);
}
asyncTasks.clear();
}
private void cancelAsyncTask(AsyncTask task) {
if (task != null) task.cancel(false);
}