在显示 ProgressDialog 时在后台加载 +暂停代码执行

发布于 2025-01-04 08:02:13 字数 1657 浏览 1 评论 0原文

我对这个冗长的标题表示歉意 - 我不确定如何准确地表达它。我正在尝试(在我的 onCreate() 方法中):

-初始化一些内容

-在从后端加载一些数据时显示进度对话框

-清除进度对话框,然后继续使用

我了解典型进度对话框的 代码解决方案是一个异步任务,我尝试使用它(见下文)。然而,这并没有像我想要的那样锁定代码执行。 lwpd.execute() 之后的代码依赖于已经发生的加载。我是否把这个问题过于复杂化了?做我想做的事的正确方法是什么?

作为参考,我的 Asynctask 实现:

public class LoadWithProgressDialog extends AsyncTask<Void, Void, Boolean>{
    private ProgressDialog pd; //the progress dialog
    private String title; //the title of the progress dialog
    private String  message; //the body of the progress dialog
    private Runnable task; //contains the code we want to run in the background
    private Context c;


    public LoadWithProgressDialog(Context context,String t, String m,Runnable r){
        super();
        c = context;
        task = r;
        title = t;
        message = m;
    }

    @Override
    protected void onPreExecute(){
        pd = ProgressDialog.show(c,title, message, false, false);
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        task.run();
        return true;
    }
   @Override
   protected void onPostExecute(Boolean result) {
            pd.dismiss(); 
   }





}

 super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            //initialize variables


            LoadWithProgressDialog lwpd = new LoadWithProgressDialog(this,"Loading","Loading Truck Data", new Runnable() {
                public void run(){
                    //code that loads things
                }
            });
            lwpd.execute();

//continue on with my code

}

I apologize for the wordy title - I wasn't sure how to phrase this exactly. I am trying to (within my onCreate() method):

-Initialize some things

-display a progress dialog while I load some data from my backend

-clear the progress dialog, and then continue on with the code

I understand the the typical progress dialog solution is an asynctask, which I have tried using (see below). However, this does not lock up code execution like I want to. The code that comes after the lwpd.execute() relies on the loading having already happened. Am I overcomplicating this? What is the correct way to do what I want to?

For reference, my Asynctask implementation:

public class LoadWithProgressDialog extends AsyncTask<Void, Void, Boolean>{
    private ProgressDialog pd; //the progress dialog
    private String title; //the title of the progress dialog
    private String  message; //the body of the progress dialog
    private Runnable task; //contains the code we want to run in the background
    private Context c;


    public LoadWithProgressDialog(Context context,String t, String m,Runnable r){
        super();
        c = context;
        task = r;
        title = t;
        message = m;
    }

    @Override
    protected void onPreExecute(){
        pd = ProgressDialog.show(c,title, message, false, false);
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        task.run();
        return true;
    }
   @Override
   protected void onPostExecute(Boolean result) {
            pd.dismiss(); 
   }





}

and

 super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            //initialize variables


            LoadWithProgressDialog lwpd = new LoadWithProgressDialog(this,"Loading","Loading Truck Data", new Runnable() {
                public void run(){
                    //code that loads things
                }
            });
            lwpd.execute();

//continue on with my code

}

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

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

发布评论

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

评论(2

淡淡の花香 2025-01-11 08:02:13

您应该将进度对话框保留在异步任务中。

数据加载后您想要执行的任何代码都可以放入 onPostExecute 方法中!

public LoadWithProgressDialog(Context context,String t, String m,Runnable r){
    super();
    c = context;
    task = r;
    title = t;
    message = m;
}

@Override
protected void onPreExecute(){
    pd = ProgressDialog.show(c,title, message, false, false);
}

@Override
protected Boolean doInBackground(Void... params) {
    task.run();
    return true;
}
@Override
protected void onPostExecute(Boolean result) {
        pd.dismiss(); 

      // PUT YOUR CODE THAT YOU WANT TO RUN AFTER THE DATA HAS LOADED HERE!
}

You should keep the progress dialog in an async task.

Any code that you want to execute after the data has loaded can be put in the onPostExecute method!

public LoadWithProgressDialog(Context context,String t, String m,Runnable r){
    super();
    c = context;
    task = r;
    title = t;
    message = m;
}

@Override
protected void onPreExecute(){
    pd = ProgressDialog.show(c,title, message, false, false);
}

@Override
protected Boolean doInBackground(Void... params) {
    task.run();
    return true;
}
@Override
protected void onPostExecute(Boolean result) {
        pd.dismiss(); 

      // PUT YOUR CODE THAT YOU WANT TO RUN AFTER THE DATA HAS LOADED HERE!
}
演多会厌 2025-01-11 08:02:13

不锁定代码执行是 AsyncTask 的重点。如果您希望它阻止代码执行,只需在主线程上创建一个 ProgressDialog

Not locking code execution is the point of an AsyncTask. If you want it to block code execution just create a ProgressDialog on the main thread

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