Last active
January 2, 2016 15:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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