วันอาทิตย์ที่ 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
-->

Thread Example

--> Thread Example Code

Threads
Every application by default runs a single process upon creation that contains all the tasks.To avoid hanging the user interface, time-consuming tasks, such as network downloads orcomputationally intensive calculations, should reside in a separate background thread. It is up to the developer to implement this properly, but then the Android operating system (OS) prioritizes the threads accordingly.
Most applications can benefit from the use of threads.

ThreadExampleActivity.java

package Android.code.thread;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class ThreadExampleActivity extends Activity {
         
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
              
        Thread t = new Thread(new Runnable() {     
           
            public void run(){  
                                               
                        try{
                              Thread.sleep(5000);                                                    
                        }
                        catch(InterruptedException e){
                        }
                        // To do
                        startActivity(new Intent(
                                    "Android.code.thread.activity2"));                                          
            }
        });
        t.start();
       
        /*
        if(t != null){
          Thread dummy = t;

      t = null;
          dummy.interrupt();

    }
        */

    }
}

 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="#55D5F5"
    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="Thread Example"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FF0000"
        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,
Wait for 5 sec."
        android:textColor="#000000"
        android:textSize="24dp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:layout_weight="0.02"
        android:src="@drawable/ic_launcher" />

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

</LinearLayout>


Activity2.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="#FF4444"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#FFBB33"
        android:text="After 5 sec, Display this activity."
        android:textColor="#000000"
        android:textSize="30dp" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
       
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="200dp"
        android:text="activity2.xml"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>




 
-->more info Handler Example


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

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