public class MyHandlerActivity extends Activity {
Button button;
MyHandler myHandler;
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
button = (Button) findViewById();
myHandler = new MyHandler();
// When creating a new Handler instance, it will be bound to the queue of the current thread and message and start distributing data
// Handler has two functions, (1): execute Message and Runnalbe objects regularly
// (2): Let an action be executed in different threads.
// It arranges the message using the following method
// post(Runnable)
// postAtTime(Runnable,long)
// postDelayed(Runnable,long)
// sendEmptyMessage(int)
// sendMessage(Message);
// sendMessageAtTime(Message,long)
// sendMessageDelayed(Message,long)
// The above method starts with post allows you to process Runnable objects
//sendMessage() allows you to process Message objects (Message can contain data,)
MyThread m = new MyThread();
new Thread(m).start();
}
/**
* Accept messages and process messages. This Handler will run together with the current main thread.
* */
class MyHandler extends Handler {
public MyHandler() {
}
public MyHandler(Looper L) {
super(L);
}
// Subclasses must rewrite this method to accept data
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
("MyHandler", "handleMessage......");
(msg);
// You can update the UI here
Bundle b = ();
String color = ("color");
(color);
}
}
class MyThread implements Runnable {
public void run() {
try {
(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
();
}
("thread.......", "mThread........");
Message msg = new Message();
Bundle b = new Bundle();// Store data
("color", "My");
(b);
(msg); // Send a message to the Handler and update the UI
}
}