Android-interaction instance (clock) between sub-thread and main thread)

Source: Internet
Author: User

Android-interaction instance (clock) between sub-thread and main thread)

The. xml Code is as follows:

 
         
  
 

The. java program code is as follows:

Package org. lxh. demo; import java. util. date; import java. text. simpleDateFormat; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. widget. textView; public class Hello extends Activity {private TextView info = null; private static final int SET = 1; private Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {switch (msg. what) {case SET: Hello.this.info. setText ("current time:" + msg. obj. toString (); break ;}}; private class ClockThread implements Runnable {// Thread class that displays time public void run () {while (true) {try {Message msg = Hello. this. handler. obtainMessage (Hello. SET, new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "). format (new Date (); Hello. this. handler. sendMessage (msg); Thread. sleep (1000);} catch (Exception e) {}}} public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // life cycle method super. setContentView (R. layout. main); // set the layout manager this.info = (TextView) super. findViewById (R.id.info); new Thread (new ClockThread ()). start (); // start thread }}
The running instance is as follows:


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.