Handler usage in Activity

Source: Internet
Author: User

Use Handler to update the background color of a TextView and change it cyclically in five colors.

Handle features:

1. Pass the Message. It is used to receive data sent by the sub-thread and use this data to update the UI with the main thread.

2. Pass the Runable object. It is used to arrange the execution sequence of different operations through the Message Queue bound by Handler.

Method 1:

Import java. util. random; import com. example. handle. r. color; import android. r. integer; import android. app. activity; import android. content. res. resources. theme; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. widget. textView; public class MainActivity extends Activity {TextView TV; int color []; Handler myHandler; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TV = (TextView) findViewById (R. id. TV); color = new int [] {R. color. black, R. color. blue, R. color. red, R. color. yellow, R. color. green}; // background color array myHandler = new Handler (); myHandler. post (rb);} Runnable rb = new Runnable () {public void run () {Random rd = new Random (); // generate a Random number int I = rd. nextInt (5); TV. setBackgroundColor (getResources (). getColor (color [I]); myHandler. postDelayed (rb, 1000); // set the delay time }};}

Method 2:

import android.app.Activity;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.os.Handler;import android.os.Message;public class SecondActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);new updateColor().start();}Handler handler=new Handler(){public void handleMessage(Message msg){Resources res = getResources();Drawable drawable = res.getDrawable(R.color.black);Drawable drawable1 = res.getDrawable(R.color.aqua);Drawable drawable2 = res.getDrawable(R.color.green);Drawable drawable3 = res.getDrawable(R.color.red);Drawable drawable4 = res.getDrawable(R.color.pink);Bundle b=msg.getData();int key=b.getInt("key");switch(key){case 0: SecondActivity.this.getWindow().setBackgroundDrawable(drawable) ;break;case 1:SecondActivity.this.getWindow().setBackgroundDrawable(drawable1);break;case 2:SecondActivity.this.getWindow().setBackgroundDrawable(drawable2);break;case 3:SecondActivity.this.getWindow().setBackgroundDrawable(drawable3);break;case 4:SecondActivity.this.getWindow().setBackgroundDrawable(drawable4);break;}}};class updateColor extends Thread{int i=0;public void run() {while(true){Message msg=handler.obtainMessage();Bundle bundle=new Bundle();if(i<5){try {bundle.putInt("key", i);msg.setData(bundle);i=i+1;handler.sendMessage(msg);Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}else{i=i%5;}}}}}

Effect:


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.