Wednesday, November 17, 2010

onPostExecute() of AsyncTask

In my old exercise "AsyncTask: perform background operations and publish results on the UI thread", doInBackground() is a infinite loop without return, such that the onPostExecute() method will not be called.

In order to make onPostExecute() taking effect, a button to stop the background task is implemented. When user click on the button, the onPostExecute() will end with and return, and onPostExecute() will be called.



Modify main.xml to add a Stop button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop"
/>
<TextView
android:id="@+id/mytext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This Text will be Turn ON/OFF triggered by AsyncTask."
/>

</LinearLayout>


Modify AndroidBackgroundAsyncTask.java
package com.exercise.AndroidBackgroundAsyncTask;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidBackgroundAsyncTask extends Activity {

TextView myText;
boolean stopRun = false;

public class BackgroundAsyncTask extends
AsyncTask<Void, Boolean, Void> {

boolean myTextOn;

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub

while(!stopRun)
{
myTextOn = !myTextOn;
publishProgress(myTextOn);
SystemClock.sleep(1000);
}
return null;
}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Toast.makeText(AndroidBackgroundAsyncTask.this,
"onPostExecute", Toast.LENGTH_LONG).show();
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
myTextOn = true;
Toast.makeText(AndroidBackgroundAsyncTask.this,
"onPreExecute", Toast.LENGTH_LONG).show();
}

@Override
protected void onProgressUpdate(Boolean... values) {
// TODO Auto-generated method stub

if (values[0]){
myText.setVisibility(View.GONE);
}
else{
myText.setVisibility(View.VISIBLE);
}
}


}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView)findViewById(R.id.mytext);

Button buttonStop = (Button)findViewById(R.id.stop);
buttonStop.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopRun = true;
}});

new BackgroundAsyncTask().execute();
}
}


Download the files.

Related article:
- ProgressBar running in AsyncTask



No comments:

Post a Comment