如何在UI线程上显示ProgressDialog

时间:2011-10-21 12:37:57

标签: android progressdialog ui-thread

我真的很想弄清楚如何在这个代码的UI线程上获得ProgressDialog给出uploadPhoto并且会感谢任何指导:

@Override
    /** Handle Upload a Photo **/
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        // Get image
        if (resultCode == RESULT_OK) {

            // ProgressDialog dialog = ProgressDialog.show(this, "", "Uploading Photo...", true, false);

            switch(requestCode) { 

                // Take Photo
                case 4001:      
                    // Upload
                    uploadPhoto(Uri.fromFile(mImageFile));
                    break;

                // Select Photo
                case 5001:

                    // Get image 
                    Uri selectedImage = imageReturnedIntent.getData();

                    // Upload
                    uploadPhoto(selectedImage);
                    break;
            }

            // Dismiss
            // dialog.dismiss();            
        }
    }

2 个答案:

答案 0 :(得分:3)

像这样使用AsyncTask:

public class NetworkTask extends AsyncTask<Object , Void, Object> {
    Context context;
    boolean shouldContinue = true;
    public ProgressDialog dialog;
    String waitMessage = "Please wait, loading data...";
    public NetworkTask(Context con){
        this.context = con;     
    }
    public void setMessage(String msg){
        waitMessage = "Please wait, "+msg;
    }
    protected void onPreExecute(){
        shouldContinue  = ConnectionUtils.isNetworkAvailable(context);
        if(shouldContinue)
            dialog = ProgressDialog.show(context, null, waitMessage, true);
        else{
            Dialog.showToast(context, Constants.NETWORK_ERROR);
            return;
        }
    }
    @Override
    protected void onPostExecute(Object result){
        if(dialog != null ){
            if(dialog.isShowing())
            dialog.dismiss();
            dialog = null;
        }           
    }
    @Override
    protected Object doInBackground(Object... params){
        //do uploading and other tasks
    }
}

并在Activity中以这种方式调用它:

NetWorkTask task = new NetWorkTask(this); //Here you can pass other params
task.execute("");

答案 1 :(得分:2)

使用AsyncTask可能。将上传照片功能放在异步任务的后台。

在预执行中启动进度对话框。

在后执行中解除/取消进度对话框。

在UI线程上执行后执行和执行前运行。

private class uploadPhoto extends AsyncTask<Void, Void, Void>{

            private ProgressDialog dialog;
        protected void onPostExecute(Void dResult) {

                dialog.cancel();

        }

        protected void onPreExecute() {


            dialog = new ProgressDialog(Myactivity.this);
            dialog.setCancelable(true);
            dialog.setMessage("uploading...");
            dialog.show();

                }

        protected Void doInBackground(Void... params) {
            // call upload photo here.
        }

    }

调用asyncTask使用

new uploadPhoto().execute();