Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ProgressBar not updating onProgress

The progress bar does not update. However it ends on completion of the task.

int totalSms = 12;
int currentSms = 0;

public class ProgressTask extends AsyncTask<String, Integer, Boolean> {

    /** progress dialog to show user that the backup is processing. */
    private ProgressDialog dialog;

    public ProgressTask(ScanInboxActivity activity) {
        dialog = new ProgressDialog(activity);
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(totalSms);
    }

    protected void onPreExecute() {
        dialog.setMessage("Scan start");
        dialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        Log.d(TAG, "in onProgressUpdate: " + values[0]); // shows actuall count in the log
        dialog.setProgress(values[0]); // does not update

                    //tried even this, does not work
        //dialog.incrementProgressBy(values[0]);
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        populateList();
    }

    protected Boolean doInBackground(final String... args) {
            try {
                Context context = getApplicationContext();
                SharedPreferences prefs = PreferenceManager
                        .getDefaultSharedPreferences(context);
                while (currentSms < totalSms) {
                    Thread.sleep(1000);
                    currentSms++;
                    publishProgress(currentSms);
                }
            } catch (IllegalArgumentException e) {
            } catch (InterruptedException e) {

            }
            return true;
        }
like image 583
harshit Avatar asked Jan 13 '23 03:01

harshit


1 Answers

dialog.setIndeterminate(true);

it should be false, otherwise the loading value will be not avaluated

like image 86
Blackbelt Avatar answered Jan 30 '23 04:01

Blackbelt