Android ProgressDialog不显示

时间:2012-12-21 16:42:55

标签: android android-alertdialog

我创建了一个AlertDialog,让用户选择从服务器下载内容。

如果用户选择下载,AlertDialog即被解除,并且ProgressDialogAsyncTask相关联。

永远不会显示ProgressDialogAlertDialog的“确定”按钮保持选中状态,直到AsyncTask的操作结束。

如果我在代码中“评论”了AsyncTask操作,则AlertDialog被正确解除,并显示ProgressDialog

我没有在真实设备上尝试过该应用程序,只考虑了模拟器。

问题出在哪?

3 个答案:

答案 0 :(得分:1)

试试这段代码可能对您有所帮助

private class allinfo extends AsyncTask<String, Void, JSONObject> {
        private ProgressDialog dialog;

        protected void onPreExecute(){
             dialog = ProgressDialog.show(collection.this, "Loading", "Loading. Please wait...", true,false);
        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            // TODO Auto-generated method stub

            return json;
        }
        protected void onPostExecute(JSONObject json)
        {
            dialog.dismiss();
            info(json);
        }

     }

答案 1 :(得分:1)

final AlertDialog d = new AlertDialog.Builder(youclassname.this)
        .setView(input)
        .setTitle(R.string.message)
        .setPositiveButton(android.R.string.ok,
                new Dialog.OnClickListener() {
                    public void onClick(DialogInterface d, int which) {
                        //Do nothing here. We override the onclick
                    }
                })
        .setNegativeButton(android.R.string.cancel, null)
        .create();

d.setOnShowListener(new DialogInterface.OnShowListener() {

    public void onShow(DialogInterface dialog) {

        Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                        startDownload();//begin downloading
                    d.dismiss();

            }
        });
    }
});
d.show();

这是startDownload部分。

private void startDownload() {
        String url ="file download link";
        Toast.makeText(dwn.this, url,Toast.LENGTH_LONG).show();
        new DownloadFileAsync().execute(url);
    }

这里是asynctask

class DownloadFileAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

    @Override
    protected String doInBackground(String... aurl) {
        int count;

    try {

    URL url = new URL(aurl[0]);
    URLConnection conexion = url.openConnection();
    conexion.connect();

    int lenghtOfFile = conexion.getContentLength();
    Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

    InputStream input = new BufferedInputStream(url.openStream());
    OutputStream output = new FileOutputStream("/sdcard/file.type");

    byte data[] = new byte[1024];

    long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress(""+(int)((total*100)/lenghtOfFile));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();






             }
             catch (Exception e) {
                // TODO Auto-generated catch block

            }

    return null;

    }
    protected void onProgressUpdate(String... progress) {
         Log.d("ANDRO_ASYNC",progress[0]);
         mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    }
}

这是进度对话框的代码

 @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Downloading PDF file");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
        }
    }

答案 2 :(得分:0)

使用ProgressDialog正面按钮在AsyncTask示例中实现

AlertDialog

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        // set title
        alertDialogBuilder.setTitle("Your Title");
        // set dialog message
        alertDialogBuilder
        .setMessage("Click yes to go to AsyncTask!")
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                new MyAsyncTaskClass().execute();
            }
        })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, just close
                // the dialog box and do nothing
                dialog.cancel();
            }
        });
        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();
        // show it
        alertDialog.show();

    }

    // Here is the start of the AsyncTask    
    class MyAsyncTaskClass extends AsyncTask<String, String, Void> {

        private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

        protected void onPreExecute() {
            progressDialog.setMessage("Dialog Message");
            progressDialog.show();
            progressDialog.setCanceledOnTouchOutside(false);
        }

        @Override
        protected Void doInBackground(String... params) {
            // TODO stuff
        }

        protected void onPostExecute(Void v) {
            this.progressDialog.dismiss();
        }
    }
}