Examples and summary of multiple android threads

Source: Internet
Author: User

Public class FirActivity extends Activity {static final int LOOPER_QUIT =-1; static final int LOOPER_BACK = 1; public Handler handler_fat = new Handler () // main thread handler, it is used to receive messages from sub-threads, and the main thread has logoff by default; {public void handleMessage (Message msg) {if (msg. what = LOOPER_BACK) // receives the message from the subthread {Log. d ("father", "father-> LOOPER_BACK") ;}}; public Handler handler_son;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Button button = (Button) findViewById (R. id. button1); final Thread thread = new Thread (new Runnable () {public void run () {Looper. prepare (); // The Sub-thread does not have a looper by default. You must enable looper; handler_son = new Handler () // The Sub-thread Handler, which is used to receive messages sent by other threads; {public void handleMessage (Message msg) {if (msg. what = LOOPER_QUIT) {Log. d ("thread", "prepare to go to death"); this. getLooper (). quit (); // end the loose loop. The subthread stops preparing because the code has been executed.} else if (msg. what = LOOPER_BACK) {Log. d ("thread", "LOOPER_BACK"); handler_fat.sendEmptyMessage (LOOPER_BACK); // send the message to the main thread;} else {Log. d ("thread", msg. what + "") ;}}}; logoff. loop (); // The logoff function starts to run. The Code Execution starts from here and will not be executed until it is finished. Log. d ("thread", "going to death") ;}}); thread. start (); // The thread starts to start the button. setOnClickListener (new View. onClickListener () {public void onClick (View v) {// TODO Auto-generated method stub Log. d ("main", thread. isAlive () + ""); // returns whether the thread is alive. Here it is true; then (44); handler_son.sendEmptyMessage (LOOPER_BACK); handler_son.sendEmptyMessage (LOOPER_QUIT); Log. d ("main", thread. isAlive () + ""); // here it is false ;}});}}


This article from the "wenjian minor notes" blog, please be sure to keep this http://wenjianboy.blog.51cto.com/7389753/1297205

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.