当按下后退按钮时如何在android中停止asynctask

时间:2013-11-30 10:05:17

标签: android android-asynctask android-activity

我有一个活动(比如Avtivity A),它运行asyncatsk并显示一些结果,这些结果会显示在另一个活动中(活动B)。一切都很好。当用户单击按钮时,任务被触发,另一个活动(活动B)与进度条一起显示。如果我按下活动B中的后退按钮(进度条仍然可见),我转到活动A.但是一段时间后(任务完成时)活动B变为可见。

当用户按下活动B中的后退按钮时,我希望停止asynctask(与活动A相关联)。

请帮助我,谢谢你的帮助

4 个答案:

答案 0 :(得分:3)

MyAsyncTask myTask=null;
myTask = new MyAsyncTask();

执行任务

myTask.execute();

停止/取消任务

public void onBackPressed()
{

    myTask.cancel(true);
}

答案 1 :(得分:1)

如果你显示结果和progressBar,并在活动B中处理Canceling,那么你可能应该在Activity B中启动你的AsyncTask。但是我建议你等到AsyncTask完成然后用bundle中的数据启动Activity B.无论哪种情况,您都可以在一个活动中处理所有与AsyncTask相关的事情。尽量不要过分重视一切。只需实现一些OnAsyncTaskFinishedListener(),如:

在AsyncTask类中:

public interface OnMyAsyncTaskCompletedListener {
    void onMyAsyncTaskCompletedListener(MyResults results);
}

。 。

@Override
protected void onPostExecute(MyResults results) {
    onMyAsyncTaskCompletedListener(results);
}

@Override
protected void onCancelled() {
    onMyAsyncTaskCompletedListener(null);
}

然后在您的Activity中实现OnMyAsyncTaskCompletedListener和

public void onServerRequestCompleted(MyResults results) {
    //Start activity passing results in Intent
}

最后,要取消正在运行的活动,请执行以下操作:

@Override
public void onBackPressed() {
    mMyAsyncTask.cancel(true);
}

答案 2 :(得分:0)

答案 3 :(得分:0)

活动B (function (i) { some async operation here that uses i })(i); 方法中,您应致电public class Upload extends Activity { String path=null; private String getpath(String path){ return path; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.upload); Button browse = (Button) findViewById(R.id.browse); Button upload = (Button) findViewById(R.id.upload); browse.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(); intent.setType("application/pdf"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select PDF"), 1); } }); upload.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { upload up = new upload(); up.execute(); } }); } public void onActivityResult(int requestCode, int resultCode, Intent result) { if (resultCode == RESULT_OK) { if (requestCode == 1) { Uri data = result.getData(); String pathe = data.getPath(); path = getpath(pathe); Toast.makeText(Upload.this, path, Toast.LENGTH_SHORT).show(); } } } private class upload extends AsyncTask<Void, Void, Void>{ ProgressDialog pd; @Override protected void onPreExecute() { pd= ProgressDialog.show(Upload.this, "Uploading", "Please Wait"); super.onPreExecute(); } @Override protected void onPostExecute(Void result) { pd.dismiss(); super.onPostExecute(result); } @Override protected Void doInBackground(Void... params) { //Toast.makeText(Upload.this, path,Toast.LENGTH_LONG).show(); String url = "http://192.168.43.50/projectpri/upload.php"; File file = new File(path); file.getAbsolutePath(); try { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost(url); MultipartEntity mpe = new MultipartEntity(); ContentBody cbfile =new FileBody(file); mpe.addPart("file", cbfile); httppost.setEntity(mpe); HttpResponse response = httpclient.execute(httppost); HttpEntity ent = response.getEntity(); System.out.println(response.getStatusLine()); } catch (Exception e) { // show error } return null; }