Use of multi-thread handler -- change the title once in 3 seconds

Source: Internet
Author: User

We have some knowledge about multithreading. Now let's further study and apply it. Use it in android to change the title (changed once every 3 seconds): You only need to write the Activity implementation. Here there are two methods, you can choose to use it according to your habits. (1) Runnable: [html] public class MainActivity extends Activity {private int count = 1; private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case 1: updateTitle (); break; default: break ;}};@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); new Thread (new TitleThread ()). start ();} protected void updateTitle () {setTitle ("my bolg" + count ++);} class TitleThread implements Runnable {@ Override public void run () {while (true) {Message message = new Message (); message. what = 1; handler. sendMessage (message); try {Thread. sleep (3000);} catch (InterruptedException e) {e. printStackTrace () ;}}}@ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. activity_main, menu); return true ;}} (2) Timer: [html] public class TimerActivity extends Activity {private int count = 1; private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case 1: updateTitle (); break; default: break ;}};@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_timer); Timer timer = new Timer (); timer. scheduleAtFixedRate (new TimerTask () {@ Override public void run () {Message message = new Message (); message. what = 1; handler. sendMessage (message) ;}}, 1000,3000);} protected void updateTitle () {setTitle ("hello" + (count ++ ));} @ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. activity_timer, menu); return true ;}}

Related Article

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.