try like this.. or look at this example it migth help you.. call onCancle() method where you want.. as shown in Onpause() in below example, It ll cancel the asyncTask
public class newA extends Activity implements OnClickListener {
private static final String TAG = "MyPost";
private boolean post_is_running = false;
private doSomethingDelayed doSth;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button pushButton = (Button) findViewById(R.id.push_button);
pushButton.setOnClickListener(this);
}
@Override
protected void onPause() {
super.onPause();
if (post_is_running) {
Log.v(TAG, "Stopping Async Task onPause");
doSth.cancel(true);
}
}
@Override
protected void onResume() {
super.onResume();
if (post_is_running) {
Log.v(TAG, "Starting Async Task onResume");
doSth = (doSomethingDelayed) new doSomethingDelayed().execute();
((Button) findViewById(R.id.push_button)).setText("Resuming..");
}
}
public void onClick(View v) {
if (post_is_running == false) {
post_is_running = true;
Log.v(TAG, "Starting Async Task onClick");
doSth = (doSomethingDelayed) new doSomethingDelayed().execute();
((Button) findViewById(R.id.push_button)).setText("Starting..");
} else {
Log.v(TAG, "Stopping Async Task onClick");
post_is_running = false;
doSth.cancel(true);
((Button) findViewById(R.id.push_button)).setText("Stopping..");
}
}
private class doSomethingDelayed extends AsyncTask<Void, Integer, Void> {
private int num_runs = 0;
private JSONObject holder;
@Override
protected Void doInBackground(Void... gurk) {
while (!this.isCancelled()) {
Log.v(TAG, "going into postData");
long ms_before = SystemClock.uptimeMillis();
Log.v(TAG, "Time Now is " + ms_before);
try {
Thread.sleep(5000);
postData();
} catch (InterruptedException e) {
e.printStackTrace();
}
long ms_after = SystemClock.uptimeMillis();
long time_passed = ms_after - ms_before;
Log.v(TAG, "coming out of postData");
Log.i(TAG, "RTT: " + time_passed + " ms");
num_runs++;
if (!this.isCancelled()) {
publishProgress(num_runs, (int) time_passed);
}
}
return null;
}
@Override
protected void onCancelled() {
Context context = getApplicationContext();
CharSequence text = "request is send every 5 sec.";
int duration = Toast.LENGTH_LONG;
Toast.makeText(context, text, duration).show();
((Button) findViewById(R.id.push_button))
.setText("Stopped. Tap to Start for sending new request!");
}
@Override
protected void onProgressUpdate(Integer... num_runs) {
Context context = getApplicationContext();
CharSequence text = "Looped with time interval in every 5 sec "
+ num_runs[0].toString() + " Times";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context,
text + "\nRTT: " + num_runs[1].toString() + " ms", duration)
.show();
((Button) findViewById(R.id.push_button)).setText(text
+ "\nTap to Stop process");
}
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.1/listen");
String resp = null;
long time_passed = 0;
// Sending details in Json Format
holder = new JSONObject();
try {
holder.put("UserId", "1");
holder.put("TimeStamp", "12345");
System.out.println("----holder--" + holder);
} catch (JSONException e1) {
e1.printStackTrace();
}
try {
StringEntity se = new StringEntity(holder.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
resp = response.toString();
String t = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
} catch (IOException e) {
Log.e(TAG, e.toString());
} catch (RuntimeException e) {
e.printStackTrace();
}
Log.i(TAG, "RTT inside post-data: " + time_passed);
Log.i(TAG, resp.toString());
}
}
}