为什么按下后退按钮时出现进度对话框?

时间:2014-01-27 21:11:10

标签: java android asynchronous progressdialog

当我按返回按钮返回上一个活动时,进度对话框出现并且不会消失。当我最小化应用程序时,进度对话框消失。

以下是异步类的代码

public class BackGroundTask extends AsyncTask<String, String, JSONObject> {
    List<NameValuePair> postparams = new ArrayList<NameValuePair>();
    private ProgressDialog pd;
    String url = null;
    String method = null;
    Context context;


    public BackGroundTask(String url, String method,
            List<NameValuePair> params, Context context) {
        this.url = url;
        postparams = params;
        this.method = method;
        this.context = context;

        //pd = new ProgressDialog(context);
        //pd.setTitle("Processing...");
        //pd.setMessage("Please wait.");
        //pd.setCancelable(false);
        //pd.setIndeterminate(true);

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(context);
        pd = ProgressDialog.show(context, "Processing...", "Please wait.", true, false);

    }

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        pd.dismiss();
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        // TODO Auto-generated method stub
        // Making HTTP request
        try {
            // Making HTTP request
            // check for request method

            if (method.equals("POST")) {
                // request method is POST
                // defaultHttpClient
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(postparams));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } else if (method == "GET") {
                // request method is GET
                HttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(postparams,
                        "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();

        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        System.out.println(json);

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        //pd.dismiss();

        // return JSON String
        return jObj;

    }


}

1 个答案:

答案 0 :(得分:0)

当你完成Activity并且不能完成任务时,你不会解雇它。 Override finish()并在必要时将其解雇

@Override
public void finish()
{
     if (pd.isShowing()
     {
          pd.dismiss();
     }
     super.finish();
}

您也可以Override onBackPressed()并将此代码放在那里,但由于按下后退按钮会调用finish(),因此可能更安全。

此外,您正在一个地方正确比较Strings

if (method.equals("POST"))  // correct

但不是其他人

else if (method == "GET")  // incorrect