简体   繁体   中英

AsyncTask progress dialog issue

I have a custom AsyncTask class and I want to use it for GCM registration and registration on My server. The registration as it is work's fine. But if i make it in AsyncTask then I can't show progress dialog while registration still in progress. Progress dialog disappears after task starts. But I want to close it only after GCM registration complete, My server regitration complete and some data uploaded to the server. May be someone can help me to make it all work. Or maybe someone could show me some more suitable variant for such scenario. GCM classes and registration methods was taken from gcm android example.

My AsyncTask class code(just for screen rotation):

public abstract class GNCustomAsyncTask<TParams, TProgress, TResult> extends AsyncTask<TParams, TProgress, TResult> {
protected GNCommunityApplication mApp;
protected Activity mActivity;

public GNCustomAsyncTask(Activity activity) {
    mActivity = activity;
    mApp = (GNCommunityApplication) mActivity.getApplication();
}

public void setActivity(Activity activity) {
    mActivity = activity;
    if (mActivity == null) {
        onActivityDetached();
    } else {
        onActivityAttached();
    }
}

protected void onActivityAttached() {}

protected void onActivityDetached() {}

@Override
protected void onPreExecute() {
    mApp.addTask(mActivity, this);
}

@Override
protected void onPostExecute(TResult result) {
    mApp.removeTask(this);
}

@Override
protected void onCancelled() {
    mApp.removeTask(this);
}
}

Registration short code:

private static void checkRegistration(final Context context) {
    final String gid = GCMRegistrar.getRegistrationId(context);
    final Integer sid = CommonUtils.getSid(context);

    if (gid.equals("")) {
        // Automatically registers application on startup.
        GCMRegistrar.register(context, SENDER_ID);
    } else {
        // Device is already registered on GCM
        if (sid == null) {
            ServerUtils.register(context, gid, CommonUtils.getImsi(context));
        }
    }
}

My AsyncTask:

private static class GetRegistrationIDTask extends GNCustomAsyncTask<Object, Integer, Integer> {

    private static final String TAG = "DoBackgroundTask";

    private ProgressDialog mProgress;
    private int mCurrProgress;
    private RegistrationActivity activity;

    public GetRegistrationIDTask(RegistrationActivity activity) {
        super(activity);
        this.activity = activity;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showProgressDialog();
    }

    @Override
    protected void onActivityDetached() {
        if (mProgress != null) {
            mProgress.dismiss();
            mProgress = null;
        }
    }

    @Override
    protected void onActivityAttached() {
        showProgressDialog();
    }

    private void showProgressDialog() {
        mProgress = new ProgressDialog(mActivity);
        mProgress.setMessage("Registering...");
        mProgress.setCancelable(false);
        mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER );
        mProgress.show();
        mProgress.setProgress(mCurrProgress);
    }

    @Override
    protected Integer doInBackground(Object... params) {
        checkRegistration(activity);
        Integer sid = CommonUtils.getSid(activity);
        return sid;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        mCurrProgress = progress[0];
        if (mActivity != null) {
            mProgress.setProgress(mCurrProgress);
        }
        else {
            Log.d(TAG, "Progress updated while no Activity was attached.");
        }
    }

    @Override
    protected void onPostExecute(Integer sid) {
        super.onPostExecute(null);
        if (mActivity != null) {
            Toast.makeText(activity, "SID: " + sid, Toast.LENGTH_LONG).show();
            mProgress.dismiss();
            GCMRegistrar.unregister(activity);
        } else {
            Log.d(TAG, "AsyncTask finished while no Activity was attached.");
        }
    }

}

Simple, Set a static boolean value true in GCMIntentService.onRegistered() method and check this boolean variable value in AsyncTask's onPostExecute() method. If it was false repeat the process of GCM Registration else dismiss progress dialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM