我有一个自定义 AsyncTask 类,我想将它用于 GCM 注册和我的服务器上的注册。注册工作正常。但是,如果我在 AsyncTask 中完成,那么在注册仍在进行时我无法显示进度对话框。任务开始后进度对话框消失。但我只想在 GCM 注册完成后关闭它,我的服务器 regitration 完成并且一些数据上传到服务器。可能有人可以帮助我完成这一切。或者也许有人可以向我展示一些更适合这种情况的变体。GCM 类和注册方法取自 gcm android 示例。
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);
}
}
注册短码:
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));
}
}
}
我的异步任务:
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.");
}
}
}