在显示 ProgressDialog 时在后台加载 +暂停代码执行
我对这个冗长的标题表示歉意 - 我不确定如何准确地表达它。我正在尝试(在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将进度对话框保留在异步任务中。
数据加载后您想要执行的任何代码都可以放入 onPostExecute 方法中!
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!
不锁定代码执行是 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