Skip to content

Instantly share code, notes, and snippets.

@elight
Created May 30, 2009 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elight/120392 to your computer and use it in GitHub Desktop.
Save elight/120392 to your computer and use it in GitHub Desktop.
package com.example.myproject;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MyActivity extends Activity
{
private final String[] LETTERS = {"A", "n", "d", "r", "o", "i", "d"};
private final String HELLO_COMMA_STR = "Hello, ";
private final Handler uiThreadCallback = new Handler();
private final int RESTART = 42;
// This lil guy is used to inject operations that effect the UI into the event loop
private final LetterAddingRunnable runInUIRunnable = new LetterAddingRunnable();
// And he does the real legwork
private BackgroundRunnable bgRunnable = null;
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.tv = new TextView(this);
runInUIRunnable.setView(tv);
start();
setContentView(tv);
}
/** Called when the view is just about to be displayed */
@Override
public void onResume() {
super.onResume();
}
/** (Re)initializes the fancy-schmancy Hello, Android and starts the background Thread */
public void start() {
if(this.bgRunnable != null) {
this.bgRunnable.reset();
runInUIRunnable.reset();
}
this.tv.setText(HELLO_COMMA_STR);
this.bgRunnable = new BackgroundRunnable();
Thread t = new Thread(this.bgRunnable);
t.start();
}
/** Creates the Restart menu item */
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, RESTART, 0, "Restart");
return true;
}
/** Handles the Restart menu item selection */
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == RESTART) {
start();
return true;
}
return false;
}
class BackgroundRunnable implements Runnable {
private boolean stop = false;
public void run() {
for(String letter: LETTERS) {
try {
Thread.currentThread().sleep(1000);
if(stop) break;
runInUIRunnable.addLetter(letter);
} catch(Exception e) {
}
}
}
public void reset() {
stop = true;
}
}
class LetterAddingRunnable implements Runnable {
private String letter;
private TextView tv;
private StringBuffer buf = new StringBuffer(HELLO_COMMA_STR);
public void reset() {
buf.setLength(0);
buf.append(HELLO_COMMA_STR);
}
public void setView(TextView tv) {
this.tv = tv;
}
public void addLetter(String letter) {
buf.append(letter);
uiThreadCallback.post(this);
}
public void run() {
tv.setText(buf.toString());
}
}
}
@newacct
Copy link

newacct commented May 6, 2011

Thread.currentThread().sleep(1000); should just be written as Thread.sleep(1000); because sleep() is a static method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment