简体   繁体   English

progressDialog和AsyncTask

[英]progressDialog and AsyncTask

I would like to create a dialog with a progress bar inside. 我想创建一个带有进度条的对话框。 But I see the Toas message, but I don't see the dialog and tre progress bar ..why? 但是我看到了Toas消息,但看不到对话框和进度条..为什么?

thx a lot for evryone. evryone非常重要。

//prepare for a progress bar dialog
            progressBar = new ProgressDialog(this);
            progressBar.setCancelable(true);
            progressBar.setMessage("Creazione Database\nIngredienti...");
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
                    progressBar.show();
            new BackgroundAsyncTask(appContext, progressBar).execute();

... ... ... ……

and i have created a generic class: 并且我创建了一个通用类:

public class BackgroundAsyncTask extends AsyncTask<Void, Integer, Void> {

    int myProgressCount;
    Context context;
    ProgressDialog progressBar;      

    public BackgroundAsyncTask(Context appContext, ProgressDialog progressBar) {
        // TODO Auto-generated constructor stub
        this.context = appContext;
        this.progressBar = progressBar;
    }

    @Override
    protected void onPreExecute() {
           Toast.makeText(context, "onPreExecute Start Progress Bar", Toast.LENGTH_LONG).show();
           progressBar.setProgress(0);
           myProgressCount = 0;
    }

    @Override
    protected Void doInBackground(Void... params) {
           // TODO Auto-generated method stub
           while (myProgressCount < 100) {
                 myProgressCount++;
                 /**
                  * Runs on the UI thread after publishProgress(Progress...) is
                  * invoked. The specified values are the values passed to
                  * publishProgress(Progress...).
                  *
                  * Parameters values The values indicating progress.
                  */

                 publishProgress(myProgressCount);
                 SystemClock.sleep(100);
           }
           return null;
    }

    /**
     * This method can be invoked from doInBackground(Params...) to publish
     * updates on the UI thread while the background computation is still
     * running. Each call to this method will trigger the execution of
     * onProgressUpdate(Progress...) on the UI thread.
     *
     * onProgressUpdate(Progress...) will not be called if the task has been
     * canceled.
     *
     * Parameters values The progress values to update the UI with.
     */
    @Override
    protected void onProgressUpdate(Integer... values) {
           // TODO Auto-generated method stub
           progressBar.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Void result) {
           Toast.makeText(context, "onPostExecute End Progress Bar",Toast.LENGTH_LONG).show();
        progressBar.dismiss();   
    }
}

The progress range is 0..10000. - as the docs says. -正如文档所说。 Use numbers that fit that range. 使用适合该范围的数字。

I highly recommend use dialog fragment instead dialog 我强烈建议使用对话框片段而不是对话框

public class ProgressDialogFragment extends DialogFragment { 公共类ProgressDialogFragment扩展DialogFragment {

public static LoadingDialogFragment newInstance() {
    ProgressDialogFragment frag = new ProgressDialogFragment ();
    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final ProgressDialog dialog = new ProgressDialog(getActivity());
    dialog.setMessage(getString(R.string.loading_text));
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);

    // Disable the back button
    OnKeyListener keyListener = new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode,
                KeyEvent event) {

            if( keyCode == KeyEvent.KEYCODE_BACK){                  
                return true;
            }
            return false;
        }


    };
    dialog.setOnKeyListener(keyListener);
    return dialog;
}

} }

More info here https://gist.github.com/dhawalhshah/1355547 更多信息在这里https://gist.github.com/dhawalhshah/1355547

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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