multithreading - Android - Using Indeterminate ProgressBar with another Thread -


i trying use indeterminate progress bar when user clicks button when email sent. having problems thread open send email , progress bar think. right crashes when 2 close , wasn't sure if there smart way enable progress bar (basically want animation of swirling circle while emails sent user has feedback happening in background).

oncreate

public void oncreate(bundle savedinstancestate){     super.oncreate(savedinstancestate);     requestwindowfeature(window.feature_indeterminate_progress);      setcontentview(r.layout.signin_page);      verify_button = (button) findviewbyid(r.id.verify_button);     signin_button = (button) findviewbyid(r.id.signin_button);      staysignedin = (checkbox) findviewbyid(r.id.staysignedincheck);      link4help = (textview) findviewbyid(r.id.link_to_register);      gmail = (edittext) findviewbyid(r.id.signin_email);     password = (edittext) findviewbyid(r.id.signin_password);      email_success = (imageview) findviewbyid(r.id.email_authenticate_success);     password_success = (imageview) findviewbyid(r.id.password_authenticate_success);      email_success.setvisibility(view.invisible);     password_success.setvisibility(view.invisible);      signin_button.setenabled(false);      verify_button.setonclicklistener(this);     signin_button.setonclicklistener(this);     link4help.setonclicklistener(this);      final float scale = this.getresources().getdisplaymetrics().density;     staysignedin.setpadding(staysignedin.getpaddingleft() + (int)(10.0f * scale + 0.5f),             staysignedin.getpaddingtop(),             staysignedin.getpaddingright(),             staysignedin.getpaddingbottom());       /* setting handler progressbar */     //b = (progressbar) findviewbyid(r.id.progressbar_verify);     setprogressbarindeterminatevisibility(false);  } 

onclick

@override public void onclick(view v) {     setprogressbarindeterminatevisibility(true);     //log.e("verify clicked","hi");     switch(v.getid()){       case r.id.verify_button:          thread thread = new thread(){             public void run(){                   string gmailstring = gmail.gettext().tostring();                 string passstring = password.gettext().tostring();                 string[] recip = new string[]{gmailstring};                 string body = "\nthis test amazing dictation2go app!   created --";                     mailaccount  = new mailaccount(gmailstring,passstring);                     try {                         isgoogleauthenticated = a.sendemailto(recip, "test", body);                     } catch (messagingexception e) {                            log.e("failed connect", "mess: "+e.getmessage());                            isgoogleauthenticated = false;                     }                            }         };         thread.start();          try {             thread.join();         } catch (interruptedexception e1) {             e1.printstacktrace();         }           log.e("end result",string.valueof(isgoogleauthenticated));          if(isgoogleauthenticated){             pb.setvisibility(view.invisible);             email_success.setvisibility(view.visible);             password_success.setvisibility(view.visible);             signin_button.setenabled(true);             password.setenabled(false);             gmail.setenabled(false);              gmail.setbackgroundresource(r.layout.bordersuccess);             password.setbackgroundresource(r.layout.bordersuccess);         }else{             gmail.settext("");             password.settext("");         }         setprogressbarindeterminatevisibility(false);         break; 

----- full solution ---------------------------------------------------------

in oncreate method of class

public void oncreate(bundle savedinstancestate){     super.oncreate(savedinstancestate);     requestwindowfeature(window.feature_indeterminate_progress);     setcontentview(r.layout.signin_page);     setprogressbarindeterminatevisibility(false); //sets default value     //look @ picture #1 screen in state  } 

in onclick method (i implemented setonclicklister activity)

@override public void onclick(view v) {      setprogressbarindeterminatevisibility(true);      switch(v.getid()){       case r.id.verify_button:           string gmailstring = gmail.gettext().tostring();         string passstring = password.gettext().tostring();          new asynctask<string,void,boolean>() {             protected void onpreexecute() {                 setprogressbarindeterminatevisibility(true);                         //look @ picture #2 screen in state             }              protected boolean doinbackground(string... args) {                 string gmailstring = args[0];                 string[] recip = new string[] { gmailstring };                 string passstring = args[1];                 string body = args[2];                  mailaccount  = new mailaccount(gmailstring, passstring);                 try {                     return a.sendemailto(recip, "test", body);                 } catch (messagingexception e) {                     log.e("failed connect", "mess: "+e.getmessage());                     return false;                 }              }              protected void onpostexecute(boolean isgoogleauthenticated) {                 setprogressbarindeterminatevisibility(false);                 if(isgoogleauthenticated){                     email_success.setvisibility(view.visible);                     password_success.setvisibility(view.visible);                     signin_button.setenabled(true);                     password.setenabled(false);                     gmail.setenabled(false);                      gmail.setbackgroundresource(r.layout.bordersuccess);                     password.setbackgroundresource(r.layout.bordersuccess);                 }else{                     gmail.settext("");                     password.settext("");                 }             }         }.execute(gmailstring, passstring, "test complete");         //look @ picture #3 screen in state          break; 

picture #1 oncreate

picture #1 oncreate


picture #2 onclick - loading

picture #2 onclick - loading


picture #3 finished loading

picture #3 finished loading

in onclick method want use asynctask. using asynctask means don't need worry writing own threading code. noted in comments join doing block main thread, potentially causing app stop responding. perhaps want along lines of;

@override public void onclick(view v) {     string gmailstring = gmail.gettext().tostring();     string passstring = password.gettext().tostring();      new asynctask<string,void,boolean>() {         protected void onpreexecute() {             setprogressbarindeterminatevisibility(true);         }          protected boolean doinbackground(string... args) {             string gmailstring = args[0];             string[] recip = new string[] { gmailstring };             string passstring = args[1];             string body = args[2];              mailaccount  = new mailaccount(gmailstring, passstring);             try {                 return a.sendemailto(recip, "test", body);             } catch (messagingexception e) {                 log.e("failed connect", "mess: "+e.getmessage());                 return false;             }          }          protected void onpostexecute(boolean isgoogleauthenticated) {             setprogressbarindeterminatevisibility(false);             // rest of ui updates here         }     }.execute(gmailstring, passstring, body); } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -