使用抽象类在AsyncTask <void,void,=“”boolean =“”>中不调用onPostExecute

时间:2017-04-06 15:19:01

标签: android android-asynctask abstract-class

我正在使用Google Calendar API并坚持使用AsyncTask。如果我使用抽象类,则无法调用onPostExecute。

MakeRequestTask.java

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

import com.google.api.client.googleapis.extensions.android.gms.auth.GooglePlayServicesAvailabilityIOException;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;

import java.io.IOException;
import java.util.List;

abstract class MakeRequestTask extends AsyncTask<Void, Void, Boolean> {
private String TAG = "MakeRequestTask";

final MainActivity mainactivity;
final com.google.api.services.calendar.Calendar mService;
private Exception mLastError = null;

ProgressDialog loading = null;

//Calendar service;
MakeRequestTask(MainActivity mainactivity) {
    this.mainactivity = mainactivity;
    this.mService = mainactivity.mService;

    loading = new ProgressDialog(mainactivity);
    loading.setCancelable(true);
    loading.setMessage("Sync with Google Calendar";
    loading.setProgressStyle(ProgressDialog.STYLE_SPINNER);

}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    Log.i(TAG, "onPreExecute");
    mainactivity.numAsyncTasks++;
    loading.show();
}


@Override
protected final Boolean doInBackground(Void... ignored) {
    Log.i(TAG, "doInBackground");
    try {
        doInBackground();
        return true;
    } catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
        mainactivity.showGooglePlayServicesAvailabilityErrorDialog(
                availabilityException.getConnectionStatusCode());
    } catch (UserRecoverableAuthIOException userRecoverableException) {
        mainactivity.startActivityForResult(
                userRecoverableException.getIntent(), MainActivity.REQUEST_AUTHORIZATION);
    } catch (IOException e) {
        //Utils.logAndShow(activity, MainActivity.TAG, e);
        Log.i(TAG, "IOException" + e);
    }
    return false;
}

@Override
protected final void onPostExecute(Boolean success) {
    super.onPostExecute(success);
    Log.i(TAG, "onPostExecute");
    loading.cancel();
    if (0 == --mainactivity.numAsyncTasks) {
    }
    if (success) {
    }
}


abstract protected void doInBackground() throws IOException;

}

AsyncLoadEvent.java(抽象类之一)

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class AsyncLoadEvent extends MakeRequestTask {
private String TAG = "AsyncLoadEvent";

AsyncLoadEvent(MainActivity mainActivity) {
    super(mainActivity);
    this.mainActivity = mainActivity;
}

@Override
protected void doInBackground() throws IOException {
    Log.i(TAG, "doInBackground");
    try {
        ......
    } catch (Exception e) {
        Log.i(TAG, "Exception" + e);
        cancel(true);
    }
}
}

从这个问题可以看出,如果我不使用抽象类并将AsyncTask java更改为如下,则将调用onPostExecute。

...
@Override
protected final Boolean doInBackground(Void... ignored) {
    Log.i(TAG, "doInBackground");
    return true;
}
...

如果有人能指出我的代码中的任何错误,我真的很感激,即使重复的解决方案也会很好,只要提供明确的解释或指导。

如果你想在我的抽象类中有更多的代码,我会这样做。

1 个答案:

答案 0 :(得分:0)

好的,经过一些努力,我找到了问题。它与我的抽象类有关。

AsyncLoadEvent.java(抽象类之一)

@Override
protected void doInBackground() throws IOException {
    Log.i(TAG, "doInBackground");
    try {
        getDataFromApi();
    } catch (Exception e) {
        Log.i(TAG, "Exception : " + e);
        cancel(true);
        Log.i(TAG, "cancel(true)");
    }
}

该类被跳入Exception并运行“cancel(true)”因为我试图更新“getDataFromApi()”中的UI组件,然后它不会调用onPostExecute()。

我在AsyncTask类中寻找Override方法,发现onCancelled()可用。并看到一个thread说:

  

调用此方法将导致调用onCancelled(Object)   在doInBackground(Object [])返回后的UI线程上。打电话给这个   方法保证永远不会调用onPostExecute(Object)。后   调用此方法时,应检查返回的值   isCancelled()定期从doInBackground(Object [])来完成   尽早完成任务。