0

鉴于uploadPhoto,我一直在努力弄清楚如何在UI线程上为这段代码获取ProgressDialog,并希望得到任何指导:

@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();            
        }
    }
4

2 回答 2

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("");
于 2011-10-21T12:50:34.233 回答
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();
于 2011-10-21T12:45:05.753 回答