【问题标题】:how cancel AsyncTask when send data发送数据时如何取消AsyncTask
【发布时间】:2016-08-22 04:45:44
【问题描述】:

它是我向服务器发送数据的代码,我想在单击返回按钮时取消Asynctask,我尝试了很多方法,但我无法取消Asynctask,该怎么做??

public class sendcomment extends AsyncTask{


@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub

    super.onPreExecute();
    pr = ProgressDialog.show(sendques.this, null, null, true);
    pr.setContentView(R.layout.progressdialog);
    pr.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));
    pr.setCancelable(true);
    pr.show();

    pr.setOnCancelListener(new ProgressDialog.OnCancelListener() {         
        @Override
        public void onCancel(DialogInterface arg0) {


            pr.cancel();


            new sendcomment().cancel(true);
        }
    });

}

    @Override
    protected Object doInBackground(Object... arg0) {
        // TODO Auto-generated method stub



        try{
            String data=URLEncoder.encode("name","utf8")+"="+URLEncoder.encode(ename.getText().toString()+"","utf8");
            data+="&"+URLEncoder.encode("email","utf8")+"="+URLEncoder.encode(eemail.getText().toString()+"","utf8");
            data+="&"+URLEncoder.encode("phone","utf8")+"="+URLEncoder.encode(ephone.getText().toString()+"","utf8");
            data+="&"+URLEncoder.encode("question","utf8")+"="+URLEncoder.encode(equestion.getText().toString()+"","utf8");
                        data+="&"+URLEncoder.encode("serial","utf8")+"="+URLEncoder.encode(phoneserialnumber+"","utf8");
            data+="&"+URLEncoder.encode("model","utf8")+"="+URLEncoder.encode(devicemodel+"","utf8");
            data+="&"+URLEncoder.encode("androidv","utf8")+"="+URLEncoder.encode(androidOS+"","utf8");


            URL link=new URL(Main.url+"getquestion.php");
            URLConnection connect=link.openConnection();


            //send data
            connect.setDoOutput(true);
            OutputStreamWriter wr=new OutputStreamWriter(connect.getOutputStream());
            wr.write(data);
            wr.flush();

            BufferedReader reader=new BufferedReader(new InputStreamReader(connect.getInputStream()));

            StringBuilder sb=new StringBuilder();

            String line=null;

            while((line=reader.readLine()) != null){

                sb.append(line);
            }
            res=sb.toString();

        }catch(Exception e){

            res=e.toString();

        }

        return "";

    }

    @Override
    protected void onPostExecute(Object result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        if(res.equals("ok")){


            Builder alert = new AlertDialog.Builder(sendques.this);
            alert.setTitle("your code: "+pnom+"");
            alert.setMessage("your comment recieved!");
            alert.setPositiveButton("ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // Finish activity
                            finish();
                        }
                    });;
            alert.show(); 


            pr.cancel();
        }


    }



}`

它是我发送数据和按下按钮发送数据的所有代码

谢谢

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

    取消 AsyncTask 可能会很痛苦 -

    代替new sendcomment().cancel(true); 试试sendcomment.this.cancel(true);

    为了测试它,要知道如果 AsyncTask 被成功取消,onPostExecute() 将不会运行但onCancelled() 会运行,所以在onCancelled() 中添加一些东西,这样你就可以知道它是否被调用,如果它被调用这意味着 AsyncTask 被取消了。

    【讨论】:

      【解决方案2】:
      new sendcomment().cancel(true);
      

      您尝试取消新任务。

      mySendCommentTask = new sendcomment();
      mySendCommentTask.cancel(true);
      

      P.S 不要使用 AsyncTask。

      【讨论】:

      • tnx,我试试这个代码sendcomment mySendCommentTask = new sendcomment(); mySendCommentTask.cancel(true); 但它不起作用
      【解决方案3】:

      试试这个

      // Initialize and execute
      sendcomment mySendCommentTask = new sendcomment();    
      mySendCommentTask.execute();
      
      // and when you need to cancel
      mySendCommentTask.cancel(true);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-18
        相关资源
        最近更新 更多