AsyncTask中的ProgressBar - 在onPostExecute方法之后显示

时间:2013-04-29 11:52:11

标签: android android-asynctask progress-bar

我通过包含AsyncTask的外部类从服务器中检索数据:

public class GetTask extends AsyncTask<String, Void, JSONObject> {

    private Context context;
    ProgressDialog dialog;

    public GetTask(Context cxt) {
        context = cxt;
        dialog = new ProgressDialog(context);
    }

    protected void onPreExecute() {
        dialog.setTitle("Load...");
        dialog.setMessage("Data...");
        dialog.show();

        super.onPreExecute();
    }

    @Override
    protected JSONObject doInBackground(String... url) {

        // code for retreive data
        return jArray;

    }

    protected void onPostExecute(JSONObject object) {
        dialog.dismiss();
        super.onPostExecute(object);
    }
}

我从我的活动中调用此任务:

Tasks task = new Tasks();
JSONObject json = task.new GetTask(this).execute(ServerURL).get();

我的数据检索成功但ProgressDialog显示在super.onPostExecute(object)之后;方法,为什么?

P.S。对话后显示:

    // Make sure the identity of this thread is that of the local process,
    // and keep track of what that identity token actually is.
    Binder.clearCallingIdentity();
    final long ident = Binder.clearCallingIdentity();
内部Looper.class上的

抱歉我的英语不好。 ))

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,需要使用回调而不是使用.get()方法。我称之为我的任务:

callTask(linkODeatails, obj);

callTask​​:

void callTask(String link, String object){
    task.new GetTask(this).execute(link + object);

}

我创建界面:

public interface AsyncTaskCompleteListener {
public void onTaskComplete(JSONObject result);

}

并添加了我的任务:

        private Activity activity;
    private AsyncTaskCompleteListener callback;

    public GetTask(Activity act){
        this.activity = act;
        this.callback = (AsyncTaskCompleteListener)act;
    }
他们叫:

callback.onTaskComplete(result, object);