Communication between android threads

Source: Internet
Author: User

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 ();}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.