在回调之后,AsyncTask ProgressDialog停止了

时间:2013-05-06 05:31:21

标签: android callback android-asynctask

这是我的任务:

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

    // callback
    private Activity activity;
    private AsyncTaskCompleteListener callback;

    private AppUtils appUtils;

    private ProgressDialog dialog;
    private String object;



    public GetTask(Activity act){
        this.activity = act;
        this.callback = (AsyncTaskCompleteListener)act;
    }

    protected void onPreExecute() {
        super.onPreExecute();

        dialog = new ProgressDialog(activity);
        dialog.setTitle("Load...");
        dialog.setMessage("Data...");
        dialog.setCancelable(true);

        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                cancel(false);

            }
        });

        dialog.show();
    }

    protected JSONObject doInBackground(String... url) {

        String link = url[0];
        object = AppUtils.cutStringAfter(link); 
        link = AppUtils.cutStringBefore(link); 
        return AppUtils.getJSONData(link);

    }

    protected void onPostExecute(JSONObject result) {
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }
        super.onPostExecute(result);
        callback.onTaskComplete(result, object);

    }

     @Override
        protected void onCancelled()
        {
            if (this.dialog != null) {
                this.dialog.dismiss();
            }
        }
}

onTaskComplete调用解析数据的其他过程:

    @Override
public void onTaskComplete(JSONObject result, String object) {
    try {
        setDBDATA(result, object);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

完成程序setDBDATA(result,object);后,对话框被解除,而工程setDBDATA,ProgressDialog冻结。

在回调程序之前,ProgressDialog有多接近,或者阻止他冻结?

2 个答案:

答案 0 :(得分:0)

如果setDBDATA需要很长时间,请将其移至doInBackground。

答案 1 :(得分:0)

opPostExecute()中的

在ProgressDialog上设置onDismissListener()并从onDismiss()调用回调方法

这样的事情: -

dialog.setOnDismissListener(new OnDismissListener() {

    @Override
    public void onDismiss(DialogInterface dialog) {
        callback.onTaskComplete(result, object);
    }
});

希望这有帮助。