0

I am working on a login in application. I am trying to display progressDialog in my BackgroundWorker class which extends Async task. But as soon as I click on Login button, the progress dialog is shown just for a few seconds. Is there any possibility that signing process will last longer?

Snippets of the 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 for a 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);
    }
}
`
Swanson
  • 103
  • 1
  • 1
  • 6

2 Answers2

0
progressDialog.dismiss();

This statement causing dialog to disappear and you have set it without any condition inside

onPostExecute

If you want to show dialog for a long time place it inside Handler or CountDownTimer. You can also make it user interaction based like button click. For example, if you want to dismiss dialog for 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();

}
payloc91
  • 3,724
  • 1
  • 17
  • 45
Exigente05
  • 2,161
  • 3
  • 22
  • 42
0

Is there any possibility that signing process will last longer? (signing = signin)

There sure is. Imagine the device is being heavily throttled, the server is taking along time to respond, or stuck on a really really slow network (ex: 1g data network, or my local starbucks wifi that 100s for people use at the same time)

You can test this by loading your application on the android emulator and using the emulator console commands for netspeed and netdelay

The basics steps are something like :

  • telnet localhost 5554
  • network speed 2 1

The first number ("2"). is the upload speed (traffic from the device) in kilobytes per second The second number ("1"), is the download speed (traffic to the device) in kilobytes per second

  • network netdelay 10000

Above, we set a 10 second delay/latency of the network

  • then on the device, perform the login

Full documentation to accomplish this is here and I recommend checking them out. Lots of great info : https://developer.android.com/studio/run/emulator-console.html

I also found this answer to be helpful: https://stackoverflow.com/a/6236379/794088

petey
  • 16,914
  • 6
  • 65
  • 97