能够成功地从服务器获取数据。如果数据成功,则导航 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);
}
}