如何在 UI 线程上显示 ProgressDialog

发布于 2024-12-11 08:16:14 字数 1079 浏览 0 评论 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();            
        }
    }

I have really been struggling figuring out how to get a ProgressDialog on the UI Thread for this code given that uploadPhoto and would appreciate any guidance:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬シ愛 2024-12-18 08:16:14

使用 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("");

Use AsyncTask something like this way:

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

and in your Activity call it like this way:

NetWorkTask task = new NetWorkTask(this); //Here you can pass other params
task.execute("");
烂人 2024-12-18 08:16:14

使用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();

use AsyncTask may be. put the upload photo function in background of the async task.

start a progress dialog in pre execute.

dismiss/cancel progress dialog in post execute.

post execute and pre execute run on UI thread.

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.
        }

    }

to call the asyncTask use

new uploadPhoto().execute();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文