1

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

如果用户选择下载,AlertDialog则关闭,并出现一个ProgressDialog连接到AsyncTask.

ProgressDialog从未显示。的“确定”按钮AlertDialog保持选中状态,直到操作结束AsyncTask

如果我在代码中“注释”了该AsyncTask操作,AlertDialog则正确解除,并ProgressDialog显示。

我还没有在真实设备上尝试过该应用程序,而只是在模拟器上尝试过。

哪个是问题?

4

3 回答 3

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);
        }

     }
于 2012-12-21T16:46:39.347 回答
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();

这是开始下载部分。

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

这是异步任务

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;
        }
    }
于 2012-12-21T18:12:38.887 回答
0

ProgressDialogAsyncTask示例中使用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();
        }
    }
}
于 2012-12-21T16:58:30.797 回答