I tried to stop AsyncTask on onPause() method.
But its not stop at all. When my App navigate to other Activity AsyncTask still on work.
this is my code... Motion1, Motion2 and Motion3 are ImageViews.
class parseComments extends AsyncTask<String, Integer,String>
{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
cancel(true);
runOnUiThread(new Runnable() {
@Override
public void run() {
centerImage.setVisibility(View.INVISIBLE);
Motion1.setVisibility(View.VISIBLE);
guiLayout1.addView(authHint1);
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
TextView textView = new TextView(guiLayout1.getContext());
mProgressBar = new ProgressBar(guiLayout1.getContext(), null, android.R.attr.progressBarStyleHorizontal);
Resources res = getResources();
mProgressBar.setProgressDrawable(res.getDrawable( R.drawable.my_progress));
//mProgressBar.setProgress(50);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams((int)(width/(float)1.5), 22);
params.setMargins(0, (int) (height /(float) 4.7) , 0, 0);
params.gravity= Gravity.CENTER_HORIZONTAL;
mProgressBar.setLayoutParams(params);
guiLayout1.addView(textView);
guiLayout1.addView(mProgressBar);
progressStatus = 60;
mProgressBar.setProgress(progressStatus);
Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(200);
progressStatus += 10;
mProgressBar.setProgress(progressStatus);
//authHint1.nextStep();
}
});
try {
Thread.sleep(timeInterval * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
Motion1.setVisibility(View.INVISIBLE);
Motion2.setVisibility(View.VISIBLE);
Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(200);
progressStatus += 10;
mProgressBar.setProgress(progressStatus);
authHint1.nextStep();
}
});
try {
Thread.sleep(timeInterval * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
Motion2.setVisibility(View.INVISIBLE);
Motion3.setVisibility(View.VISIBLE);
Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(200);
progressStatus += 10;
mProgressBar.setProgress(progressStatus);
authHint1.nextStep();
}
});
try {
Thread.sleep(timeInterval * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute() {
}
@Override
protected void onPreExecute() {
}
}
And onPause() I called
new parseComments().cancel(true);
But its not working.
How can i stop it?