android - AsyncTask progress dialog issue -
i have custom asynctask class , want use gcm registration , registration on server. registration work's fine. if make in asynctask can't show progress dialog while registration still in progress. progress dialog disappears after task starts. want close after gcm registration complete, server regitration complete , data uploaded server. may can me make work. or maybe show me more suitable variant such scenario. gcm classes , registration methods taken gcm android example.
my asynctask class code(just 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 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 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 attached."); } } }
simple, set static boolean value true in gcmintentservice.onregistered() method , check boolean variable value in asynctask's onpostexecute() method. if false repeat process of gcm registration else dismiss progress dialog.
Comments
Post a Comment