Skip to content

Instantly share code, notes, and snippets.

@shallowlong
Last active January 2, 2016 15:08
Show Gist options
  • Save shallowlong/8321107 to your computer and use it in GitHub Desktop.
Save shallowlong/8321107 to your computer and use it in GitHub Desktop.
Here gives the very basic handler idea for communicating between the main thread and others
// Handler is used to send/receive and operate for the messages
// Usually a new thread will be created to do something logic instead in the current main thread
// Handler will act as the postman between the main thread and separated thread
android.os.Handler.Handler
android.os.Message
java.lang.Thread
// declare a message handler to handle the receiving messages
Handler msgHandler = new Handler() {
// to override the handleMessage method
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
break;
case 2:
break;
default:
break;
}
}
};
// in other new thread
new Thread() {
@Override
public void run() {
// do some logic
int what = 1;
msgHandler.sendMessage(msgHandler.obtainMessage(what));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment