我有一个自定义的AsyncTask类,我想用它在我的服务器上进行GCM注册和注册。注册工作没问题。但是如果我在AsyncTask中创建它,那么在注册仍在进行时我无法显示进度对话框。任务开始后,进度对话框消失。但是我想在GCM注册完成后关闭它,我的服务器注册完成并将一些数据上传到服务器。可能有人可以帮助我完成所有工作。或者也许有人可以告诉我一些更适合这种情况的变体。 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));
}
}
}
我的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.");
}
}
}
答案 0 :(得分:0)
简单,在static boolean
方法中设置true
值GCMIntentService.onRegistered()
,并在AsyncTask's onPostExecute()
方法中检查此布尔变量值。如果false
重复GCM注册过程,则忽略进度对话框。