In this exercise, a new class, BackgroundThread, was created by extending Thread class. The object of this class run in background thread. A TextView will be toggled between on and off by this background thread. In Android, only the UI thread can modify UI elements, the method run in background thread cannot modify UI elements directly. So we have to implement a Handler, the background thread will send a Message to the Handler to change Visibility of the TextView.
main.xml
<?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"
/>
<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 a background thread."
/>
</LinearLayout>
AndroidBackgroundThread.java
package com.exercise.AndroidBackgroundThread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidBackgroundThread extends Activity {
BackgroundThread backgroundThread;
TextView myText;
boolean myTextOn = true;
public class BackgroundThread extends Thread {
boolean running = false;
void setRunning(boolean b){
running = b;
}
@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
while(running){
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendMessage(handler.obtainMessage());
}
}
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
//super.handleMessage(msg);
if (myTextOn){
myTextOn = false;
myText.setVisibility(View.GONE);
}
else{
myTextOn = true;
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);
Toast.makeText(this, "onCreate()", Toast.LENGTH_LONG).show();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
backgroundThread = new BackgroundThread();
backgroundThread.setRunning(true);
backgroundThread.start();
Toast.makeText(this, "onStart()", Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
boolean retry = true;
backgroundThread.setRunning(false);
while(retry){
try {
backgroundThread.join();
retry = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(this, "onStop()", Toast.LENGTH_LONG).show();
}
}
Download the files.
another version: Android background thread, by implementing Thread object, provided with a Runnable object.