วันอาทิตย์ที่ 1 กรกฎาคม พ.ศ. 2555

Handler Example

--> Android Handler Example Code

Handlers : Messages Between Threads
After multiple threads run concurrently, such as a main application thread and a background thread, there needs to be a way to communicate between them. Some examples are 

  • A main thread serves time-critical information and passes messages to the background time-consuming thread to update. 
  • A large computation completes and sends a message back to the calling thread with the result.
This can be accomplished with handlers, which are objects for sending messages between threads. Each handler is bound to a single thread, delivering messages to it and executing commands from it.
 
HandlerExampleActivity.java
package android.code.handler;

import android.app.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

public class HandlerExampleActivity extends Activity {
     
      private Handler hd;
      public int count=1;
      Boolean red = true,yellow=false,green = false;
      private ImageView light1,light2,light3;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        hd = new Handler();       
        final TextView tv = (TextView)findViewById(R.id.tv);
       
        light1 = (ImageView) findViewById(R.id.imageView1);
        light2 = (ImageView) findViewById(R.id.imageView2);
        light3 = (ImageView) findViewById(R.id.imageView3);
  
        Thread t = new Thread(new Runnable() {     
           
            public void run(){  
           
                  while (true){
                 
                        try{
                              Thread.sleep(2000);                                                                                        
                        }
                        catch(InterruptedException e)
                        { }
                                         
                        hd.post(new Runnable(){                        
                                                     
                              public void run(){  
                                   
                                    tv.setText("Thread Handler Running " + count++);
                                   
                                    if((red==true)&&(yellow==false)){
                                          light1.setImageResource(R.drawable.red);
                                          light2.setImageResource(R.drawable.off);
                                          light3.setImageResource(R.drawable.off);
                                          red = false;
                                          yellow = true;                                       
                                    }
                                    else if ((red==false)&&(yellow==true)){
                                          light1.setImageResource(R.drawable.off);
                                          light2.setImageResource(R.drawable.yellow);
                                          light3.setImageResource(R.drawable.off);
                                          red = false;
                                          yellow = false;                                      
                                    }
                                    else {
                                          light1.setImageResource(R.drawable.off);
                                          light2.setImageResource(R.drawable.off);
                                          light3.setImageResource(R.drawable.green);
                                          red = true;
                                          yellow = false;
                                         
                                    }
                              }
                        });
                                               
                  } // while
            }
        });
        t.start();
    }
}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF6666"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="Handler Example"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#0000FF"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#AA66CC"
        android:text="Thread Running"
        android:textColor="#000000"
        android:textSize="24dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="Hanlder to change image"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#669900" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"    
        android:src="@drawable/off" />
   
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"             
        android:src="@drawable/off" />
   
    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"           
        android:src="@drawable/off" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="50dp"
        android:text="main.xml"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#0000FF"
        android:textSize="26dp" />

</LinearLayout>



Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android
-->