Boolean Value Does Not Change On Clicking Cancel Button - How To Stop Asynctask - Android?
I am a beginner, so please explain your answer clearly. Thanks EDIT 2 I now have public class MainActivity extends Activity { //other declarations public ReadTask
Solution 1:
Hi i made some changes in your code with explanation please check.
publicclassAsynextendsActivity {
publicReadTask readTask;
// keep global to prevent creating new async task on every time button clickboolean isCancelled = true;
TextView results1;
Button start, end;
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qww);
start = (Button) findViewById(R.id.button1);
end = (Button) findViewById(R.id.button2);
start.setOnClickListener(test);
end.setOnClickListener(cancel);
results1 = (TextView) findViewById(R.id.textView1);
}
privateOnClickListener test = newOnClickListener() {
@OverridepublicvoidonClick(View V) {
if (isCancelled) { // checked to prevent creating new async task on// every time button clickToast.makeText(Asyn.this, "new task started", Toast.LENGTH_LONG)
.show();
readTask = newReadTask();
readTask.execute();
isCancelled = false;
} else {
Toast.makeText(Asyn.this, "Task already runing",
Toast.LENGTH_LONG).show();
}
}
};
privateOnClickListener cancel = newOnClickListener() {
@OverridepublicvoidonClick(View V) {
if (null != readTask) {
Toast.makeText(Asyn.this, "Task stop", Toast.LENGTH_LONG)
.show();
readTask.cancel(true);
isCancelled = true;
}
results1.setText("");
}
};
privateclassReadTaskextendsAsyncTask<Void, String, Void> {
protectedvoidonProgressUpdate(String... values) {
super.onProgressUpdate(values);
results1.setText(values[0].toString());
}
@OverrideprotectedVoiddoInBackground(Void... params) {
try {
for (int k = 0; k <= 10; k++) {
// bunch of codeLog.w("5.newString", k + "");
String isC1 = String.valueOf(isCancelled);
Log.w("5.Is cancelled?", isC1); // at this point, VALUE IS//below line for instant update as soon as the value is changedpublishProgress(" "+k + "");
// use thread to see the update otherwise remove itThread.sleep(2000);
if (isCancelled() || isCancelled == true) {
Log.d("Entered", "WHY");
break;
}
// bunch of code
}
} catch (Exception e) {
e.printStackTrace();
}
returnnull;
}
@OverrideprotectedvoidonPostExecute(Void result) {
// TODO Auto-generated method stubsuper.onPostExecute(result);
// assign true to isCancelled so that user can start task again
isCancelled = true;
}
}
}
Solution 2:
It's not working because each time you are creating new object from ReadTask
. if you want to cancel existing job. you should do this :
publicclassMainActivityextendsActivity {
ReadTask readTask;
@OverridepublicvoidonCreate(Bundle b) {
...
readTask = newReadTask();
btnStart.setOnClickListener(newOnClickListener() {
@OverridevoidonClick(View v) {
readTask.execute();
}
}
btnCancel.setOnClickListener(newOnClickListener() {
@OverridevoidonClick(View v) {
readTask.cancel();
}
}
}
}
Solution 3:
new ReadTask().onCancelled();
new ReadTask().cancel(true);
this part of your code is wrong for both of them refers to different instance of ReadTask class. you can declare a global variable of ReadTask class
public ReadTask rt;
and initialize it on your onCreate()
method.
rt = new ReadTask();
then you can already use it on every part of your activity
start it using rt.execute()
and stop it using rt.cancel(true)
Post a Comment for "Boolean Value Does Not Change On Clicking Cancel Button - How To Stop Asynctask - Android?"