0

能够成功地从服务器获取数据。如果数据成功,则导航 NextScreen.1。如果在获取数据时按下后退按钮,则 AsyncTask 对话框消失,但一段时间后它会导航到 Nextscreen 活动。所以如果我按 backbuttob 异步任务必须取消,它不应该导航到下一个活动。

    public class Screenone extends Activity {
    EditText user,pass;
    Button login;           
    InputStream is;
    String productResponse;
    EditText edit;   
    int responseCode;               

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);


    requestWindowFeature(Window.FEATURE_NO_TITLE);  
    setContentView(R.layout.mainlog);       

    user = (EditText) findViewById(R.id.user);
    pass =  (EditText) findViewById(R.id.password);     
    login = (Button) findViewById(R.id.login);

   }    

    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub      

            if ((user.getText().length() > 0)
                    && (pass.getText().length() > 0) ) {                
                new Load().execute(Status);
            }
                    }
    });

   }


   private class Load extends AsyncTask<String, String, String> {

    private ProgressDialog Dialog = new ProgressDialog(LoginScreen.this);
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        Dialog.setMessage("Please wait...");
        Dialog.show();
    }

    protected String doInBackground(String... status) { 
                try {
            HttpClient httpclient = new DefaultHttpClient();                     

     HttpPost post = new HttpPost("---url---");                     
            responseCode =httpResponse.getStatusLine().getStatusCode();

        }           


        catch (Exception e) {           
                            }                     
        return null;
    }


    protected void onPostExecute(String Result) {
        // TODO Auto-generated method stub
        super.onPostExecute(Result);      

            if (responseCode==200) {            
            Dialog.dismiss();           

            Intent intent =new Intent(Screenone.this, NextScreen.class);
            startActivity(intent);                          
                        }           

                            }               
            }


   public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

     moveTaskToBack(true);

   }

   }
4

2 回答 2

0

在从当前 Activity 移回之前,您需要取消 AsyncTask、关闭 ProgressDialog 并中止 HttpPost。试试看:

@Override
public void onBackPressed() {
     if(Load!=null){
        if(!Load.getStatus() == AsyncTask.Status.FINISHED){
           // first abort Httppost request 
           // post.abort();   
           httpclient.getConnectionManager().shutdown();

           // now dismiss ProgressDialog
            if (Dialog!=null&&Dialog.isShowing()) {
                 Dialog.dismiss();
             }
           // now cancel AsyncTask if running
            if(Load.getStatus() == AsyncTask.Status.RUNNING){
              Load.cancel(true);
             }
         }
     }
}
于 2013-02-13T18:00:23.023 回答
0

好的,问题是您没有参考您的异步任务。尝试在 Activity 中为 asynctask 创建一个属性:

private Load myAsyncTask;

现在在您的 onClick 方法中:

myAsyncTask=new Load();

myAsyncTask.execute(Status);

最后在你的 onBackPressed 中:

 public void onBackPressed() {
    super...
    if(myAsyncTask!=null)
         myAsyncTask.cancel(true);
 }
于 2013-02-13T11:36:16.350 回答