Communication between android threads
In Android, threads are divided into threads with message loops and threads without message loops. Threads with message loops generally have a logoff. Our main thread (UI thread) is a message loop thread. For this message loop mechanism, we introduce a new mechanism Handler. If we have a message loop, we need to send the corresponding message to the message loop, custom messages generally have their own processing, send and clear messages, and encapsulate these in Handler. Note that Handler only targets those threads with logoff, whether it's a UI thread or a sub-thread, as long as you have logoff, I can add something to your message queue and handle it accordingly.
The creation of a Handler will be bound to the Message Queue of this thread. If it is created in the main thread, you do not need to write code to create the message queue, the default message queue is created in the main thread. However, if it is in a subthread, the thread message queue must be initialized before Handler is created.
Example:
Package com. example. android_handle; import java. io. bufferedInputStream; import java. io. inputStream; import java.net. URL; import java.net. URLConnection; import org. apache. http. util. byteArrayBuffer; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. logoff; import android. OS. message; import android. text. method. scrollingMovementMethod; import android. util. log; impor T android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {private Handler mHandler; private Button start; private TextView text; private int I = 0; private Handler handler; private NewThread thread = new NewThread (); private String s =; @ Override protected void onCreate (Bundle savedInstance State) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); start = (Button) findViewById (R. id. start); text = (TextView) findViewById (R. id. text); text. setMovementMethod (ScrollingMovementMethod. getInstance (); handler = new Handler () {@ Override public void handleMessage (Message msg) {// TODO Auto-generated method stub s = s + msg. obj +; text. setText (s); Message childMsg = MHandler. obtainMessage (); childMsg. obj = mHandler. getLooper (). getThread (). getName () + says Hello; mHandler. sendMessage (childMsg) ;}}; thread. start (); start. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {if (mHandler! = Null) {// send the Message to the subthread Message childMsg = mHandler. obtainMessage (); childMsg. obj = mHandler. getLooper (). getThread (). getName () + says Hello; mHandler. sendMessage (childMsg) ;}}) ;}private class NewThread extends Thread {private low.mlow.private String content; public void run () {Looper. prepare (); mloe = logoff. myLooper (); mHandler = new Handler (mloler) {@ Override public void handleM Essage (Message msg) {// TODO Auto-generated method stub try {sleep (1000); // URL uri = new URL (http: // 112.74.78.53/scut-lib // + library/hotReading. php); // URLConnection ucon = uri. openConnection (); // ucon. setConnectTimeout (10000); // InputStream is = ucon. getInputStream (); // BufferedInputStream bis = new BufferedInputStream (is); // ByteArrayBuffer baf = new ByteArrayBuffer (100); // int current = 0; // While (current = bis. read ())! =-1) {// baf. append (byte) current); //} // content = new String (baf. toByteArray (), UTF-8);} catch (Exception e) {// content = e. toString () ;} I ++; Message msg1 = Message. obtain (); msg1.obj = I +; handler. sendMessage (msg1) ;}}; logoff. loop ();}}}