Skip to content

Android.app.ProgressDialog

A dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time. The dialog can be made cancelable on back key press.

The progress range is 0..10000.

Android에서 Cancel 처리 방법에 대한 예제

아래코드는 AsyncTask에서 ProgressDialog가 cancel 버튼을 클릭했을때, cancel 처리를 하는 예제이다.

class MyTask extends AsyncTask<Void, Void, Void> {
    private volatile boolean running = true;
    private final ProgressDialog progressDialog = null;

    public MyTask(Context ctx) {
        // progressDialog = gimmeOne(ctx);

        progressDialog.setCancelable(true);
        progressDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // actually could set running = false; right here, but I'll
                // stick to contract.
                cancel(true);
            }
        });
    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
    }

    @Override
    protected void onCancelled() {
        running = false;
    }

    @Override
    protected Void doInBackground(Void... params) {
        while (running) {
            // does the hard work
        }
        return null;
    }
    // ...
}

See also

Favorite site

References


  1. Android_download_progress.pdf