java - ProgressDialog in AsyncTask in android -


i working on login in application. trying display progressdialog in backgroundworker class extends async task. click on login button, progress dialog shown few seconds. there possibility signing process last longer?

snippets of code

`public class backgrroundworker extends asynctask<string,void,string>{      context context;     alertdialog alertdialog;     boolean correct;     public backgrroundworker(context context){         this.context = context;     }     @override     protected string doinbackground(string... params) {         string type = params[0];         string login_url = "random";         string register_url = "random";         if(type.equals("login")){             try {                 string username = params[1];                 string password = params[2];                 url url = new url(login_url);                 httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection();                 httpurlconnection.setrequestmethod("post");                 httpurlconnection.setdooutput(true);                 httpurlconnection.setdoinput(true);                 outputstream outputstream = httpurlconnection.getoutputstream();                 bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(outputstream, "utf-8"));                 string post_data = urlencoder.encode("username","utf-8") + "=" +urlencoder.encode(username,"utf-8")+ "&"                     +urlencoder.encode("password","utf-8") + "=" +urlencoder.encode(password,"utf-8");                 bufferedwriter.write(post_data);                 bufferedwriter.flush();                 bufferedwriter.close();                 outputstream.close();                 inputstream inputstream = httpurlconnection.getinputstream();                 bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream,"iso-8859-1"));                 string result = "";                 string line = "";                 correct = false;                 while((line = bufferedreader.readline()) != null){                     result += line;                 }                 bufferedreader.close();                 inputstream.close();                 httpurlconnection.disconnect();                 system.out.println(result);                 if (result.equalsignorecase("login success"))                     correct=true;                 return result;             } catch (malformedurlexception e) {                 e.printstacktrace();             } catch (ioexception e) {                 e.printstacktrace();             }          }         return null;     }      @override     protected void onpreexecute() {         progressdialog = progressdialog.show(context, "login",                 "please wait while.", true);         alertdialog = new alertdialog.builder(context).create();         alertdialog.settitle("login status");        }      @override     protected void onpostexecute(string result) {         if(!result.equals("login success")) {             alertdialog.setmessage(result);             alertdialog.show();          }         progressdialog.dismiss();     }      @override     protected void onprogressupdate(void... values) {         super.onprogressupdate(values);     } } ` 

progressdialog.dismiss();

this statement causing dialog disappear , have set without condition inside

onpostexecute

if want show dialog long time place inside handler or countdowntimer. can make user interaction based button click. example, if want dismiss dialog 5 sec,

@override     protected void onpostexecute(string result) {     if(!result.equals("login success")) {         alertdialog.setmessage(result);         alertdialog.show();      }     new countdowntimer(5000, 1000) {      public void ontick(long millisuntilfinished) {      }      public void onfinish() {        progressdialog.dismiss();    }   }.start();  } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -