multithreading - Android UI Thread Block when calling AsynchTask.get() -
my activity login page. when user clicks, asynctask check in database if credentials good. during task, want display progressdialog after clicking on button, stays pressed 5 seconds , progressdialog quiclky shows (less 1 second) , toast appears.
there onclick function :
button connect = (button)findviewbyid(r.id.connectbutton); final edittext logined = (edittext) findviewbyid(r.id.login); final edittext passworded = (edittext) findviewbyid(r.id.password); connect.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view arg0) { string login = logined.gettext().tostring(); string password = passworded.gettext().tostring(); string[] params = {login, password}; doasynclogin doasynclogin = new doasynclogin(); seek { string result = doasynclogin.execute(params).get(); toast.maketext(mainactivity.this, result, toast.length_long).show(); } grab (interruptedexception e) { e.printstacktrace(); } grab (executionexception e) { e.printstacktrace(); } } });
and asynctask :
private class doasynclogin extends asynctask<string, void, string> { progressdialog connectionprogressdialog = new progressdialog(mainactivity.this); @override protected string doinbackground(string... params) { homecoming getlogindata(params); } protected void onpreexecute(){ connectionprogressdialog.setprogressstyle(progressdialog.style_spinner); connectionprogressdialog.setmessage("logging in..."); connectionprogressdialog.show(); } protected void onpostexecute(string result) { connectionprogressdialog.dismiss(); } }
any ideas ?
thanks !
the problem waiting (blocking) asynchtask finish execution on main thread ( makes useless ): see documentations asynchtask method here :asynchtask.get()
instead should utilize onpostexcute
phone call results.
code:
@override public void onclick(view arg0) { string login = logined.gettext().tostring(); string password = passworded.gettext().tostring(); string[] params = {login, password}; doasynclogin doasynclogin = new doasynclogin(); doasynclogin.execute(params); }
and in asynchtask:
protected void onpostexecute(string result){ connectionprogressdialog.dismiss(); toast.maketext(mainactivity.this, result, toast.length_long).show(); }
android multithreading android-asynctask ui-thread
No comments:
Post a Comment