I am working on AsyncTask
.
I am trying to stop the AsyncTask
.
authenticationArrows mTask = new authenticationArrows();
mTask.execute();
I am trying to stop it by
mTask.cancel(true);
But it not stop.
My AsyncTask
code is below.
class authenticationArrows extends AsyncTask<Void, Void, Boolean>
{
@Override
protected void onCancelled() {
super.onCancelled();
cancel(true);
// ask if user wants to try again
}
@Override
protected Boolean doInBackground(Void... params) {
return null;
}
protected void onPostExecute() {
}
@Override
protected void onPreExecute() {
if (!isCancelled()){
handler.post(new Runnable() {
@Override
public void run() {
centerImage.setVisibility(View.INVISIBLE);
tiltMotion1.setVisibility(View.VISIBLE);
progressStatus = 60;
mProgressBar.setProgress(progressStatus);
Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(200);
progressStatus += 10;
mProgressBar.setProgress(progressStatus);
}
});
}
if (!isCancelled()){
handler.postDelayed(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);
startHandler();
}
}, 1000);
}
}
public void startHandler(){
if (!isCancelled()){
handler.postDelayed(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);
}
}, 1000);
}
}
}
How can I stop or cancel it when navigate to other activity or when App goes to background?