简体   繁体   中英

Android asynctask show progress dialog until function has finished

I know this answer has already been answered multiple times but I cannot get it to work properly. It's starts with activity A where a user needs to login via the function authenticate. When the user hits the button login I want the progressdialog to appear until the function authenticate has got a response. While success you go to another activity where you can navigate through the app.. Anyway sometimes when the internet is slow or if you're on mobile internet I still see activity A for a few seconds doing nothing while the auth function is going on. When it's done the system hops to activity B.

I tried using sleep thread thingy but that's not the point.. I want the progressdialog to appear when the user hits login and dissapear when the auth function has been finished.

With the help of AsyncTask I sometimes see the dialog for one flash of a second but not how it should be.. I also want to use asynctask for loading my listviews but the dialog isn't doing what it should.

Here my AsyncTask code

public class HeavyWorker extends AsyncTask<String, Context, Void> {

private ProgressDialog dialog;

public HeavyWorker() {
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog = new ProgressDialog(AuthenticationActivity.this);
    dialog.setMessage("Gettting data");
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.show();
}

@Override
protected Void doInBackground(String... params) {
        authenticate(username, password, autoLogin);

    return null;
}

@Override
protected void onPostExecute(Void result) {
    if (dialog != null && dialog.isShowing()) {
        dialog.dismiss();
    }
}

}

And where I call the asynctask

builder.setTitle(R.string.dialog_authenticate)
                .setIcon(R.drawable.ic_launcher)
                .setView(viewInflater)
                .setPositiveButton(R.string.dialog_button_login,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                EditText ETusername = (EditText) viewInflater.findViewById(R.id.username);
                                EditText ETpassword = (EditText) viewInflater.findViewById(R.id.password);

                                CheckBox optionAutoLogin = (CheckBox) viewInflater
                                        .findViewById(R.id.autoLogin);

                                checkAutoLogin = 0;
                                if (optionAutoLogin.isChecked()) checkAutoLogin = 1;
                                username = ETusername.getText().toString();
                                password = ETpassword.getText().toString();

                                new HeavyWorker().execute();
                                //authenticate(ETusername.getText().toString(), ETpassword.getText().toString(), checkAutoLogin);
                            }
                        })

Any help or advice is appreciated.

Are you finishing the activity A when authentication success? In that case, you could skip onPostExecute

The Documentation gives you quite a good example: You can post the Progress in the onProgressUpdate(...) And can update it from doInBackground via publishProgress(int i)

   private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         //DO YOUR PROGRESSBAR THINGS HERE
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM