我正在尝试在后台制作一个简单的任务,并在完成时显示进度条。
这是主要(也是唯一)活动的代码:
public class Login extends Activity {
public static ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Sending data...");
progressDialog.setCancelable(false);
(...)
// In some onClick Eevent..
JSONObject result = new Urltasks().execute(...).get();
(...)
}
}
这是活动的代码:
class Urltasks extends AsyncTask<String, Integer, JSONObject>{
protected void onPreExecute() {
System.out.println("Inicia onPreExecute");
Login.progressDialog.show();
}
protected void onPostExecute(String result) {
Login.progressDialog.dismiss();
}
protected JSONObject doInBackground(String... arg0) {
// Some work being done. I do not use Login.progressDialog here
}
}
使用此代码,ProgressDialog会在任务结束时显示,并且不会消除。
答案 0 :(得分:2)
问题出在这里:
JSONObject result = new Urltasks().execute(...).get();
在get()
上调用AsyncTask
会阻止当前线程,即您的UI线程,直到执行AsyncTask。因此,进度对话框无法运行。删除get()
。
要获得AsyncTask的结果,您可以例如将侦听器回调传递给asynctask,该asynctask在结果可用时得到通知:
class YourAsyncTask extends AsyncTask<ParamType, ProgressType, ResultType> {
private YourResultListener mListener;
interface YourResultListener {
void onResultAvailable(ResultType result);
}
YourAsyncTask(YourResultListener listener) {
mListener = listener;
}
@Override protected ResultType doInBackground(ParamType... params) {
//...
}
@Override protected void onPostExecute(ResultType result) {
mListener.onResultAvailable(result);
}
}
您可以像以下一样使用它:
mProgressDialog.show();
new YourAsyncTask(new YourResultListener() {
@Override void onResultAvailable(ResultType result) {
mProgressDialog.dismiss();
// use result
}).execute(params);
我个人喜欢将用户界面元素(如进度对话框)与异步任务分离。
答案 1 :(得分:0)
不要将进度对话框放在课堂上的日志中。将其保留在异步任务中
这是您展示对话框的方式
progressDialog = ProgressDialog.show(yourActivityHERE.this, "",
"Loading... please wait.");
为您编辑:
class Urls extends AsyncTask<String, Integer, JSONObject>{
ProgressDialog progressDialog;
protected void onPreExecute() {
progressDialog = ProgressDialog.show(yourActivityHERE.this, "",
"Loading... please wait.");
}
protected void onPostExecute(String result) {
progressDialog.dismiss();
}
protected JSONObject doInBackground(String... arg0) {
// Some work being done. I do not use Login.progressDialog here
}
}
答案 2 :(得分:0)
在Urltasks中创建构造函数,发送调用类的上下文。 然后使用该上下文在Urltasks的preExecute中创建进度对话框
class Urltasks extends AsyncTask<String, Integer, JSONObject>{
Context mContext;
public static ProgressDialog progressDialog;
public UrlTasks(Context c){
mContext=c;
}
protected void onPreExecute() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Sending data...");
progressDialog.setCancelable(false);
progressDialog.show();
}
protected void onPostExecute(String result) {
progressDialog.dismiss();
}
protected JSONObject doInBackground(String... arg0) {
// Do your work here
}
}