Android ProgressDialog结束得太早

时间:2017-04-30 20:08:32

标签: android android-asynctask progressdialog

我正在运行AsyncTask,会打开HttpConnection并使用它在doInBackground函数中下载JSON。

这是我的AsyncTask:

    public class AsyncQueryTask extends AsyncTask<URL, Void, String>{
    String jsonString = "";
    ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(LoginActivity.this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Getting Ready...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(URL... urls) {
        URL restURL = urls[0];
        try {
            //--------------------------------------------
            // Some initialization
            //--------------------------------------------
            HttpURLConnection urlCon = (HttpURLConnection) restURL.openConnection();
            InputStream in = urlCon.getInputStream();
            Scanner scan = new Scanner(in).useDelimiter("\\A");
            jsonString = scan.hasNext()? scan.next() : "";

            try{
                JSONArray bookArr = new JSONArray(jsonString);
                for(int i=0;i<bookArr.length();i++){
                    // Some JSON string processing...
                }

            } catch(Exception e){
                Log.e("Err", "@Activity");
                e.printStackTrace();
            }
            //----------------------------------------------------

            return jsonString;
        } catch(IOException ioe){
            ioe.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        progressDialog.dismiss();
    }
}

我在这里称呼它:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        URL url = // URL for fetching JSON string
        if (/*Some condition*/) {
            //----------------------------------------------
            // Some other tasks
            //----------------------------------------------
            AsyncQueryTask fbqt = new AsyncQueryTask();
            fbqt.execute(url);

            Intent intent = new Intent(context, NextActivity.class);
            startActivity(intent);
         }
    }

如您所见,我在onPreExecute()中正确初始化progressDialog,在dismiss()onPostExecute()初始化。

问题是进度对话框只显示了几分之一秒,并立即结束,导致应用转移到下一个活动,而没有完全执行它在doInBackground()部分。

在完成doInBackground()线程之前,我该怎么做才能使progressDialog持续到位?

2 个答案:

答案 0 :(得分:1)

你的方式会导致窗户泄漏。 Intent intent = new Intent(context, NextActivity.class); startActivity(intent); 将上面的代码放在onPostExecute()方法中。

答案 1 :(得分:0)

我认为是因为:

 AsyncQueryTask fbqt = new AsyncQueryTask();
 fbqt.execute(url);

 Intent intent = new Intent(context, NextActivity.class);
 startActivity(intent);

您正在ProgressDialogLoginActivity进行午餐,但在执行fbqt后,您启动了另一项活动(NextActivity),该活动将LoginActivity置于暂停周期,所以我想这会关闭对话框!

注意:我不了解您的代码逻辑,但我认为您在从NextActivity获取结果后尝试启动fbqt,如果是,那么您实施的内容是错误的,您应该使用接口从AsyncTask获取回调,然后启动NextActivity