能举个例子吗?

解决方案 »

  1.   

    例如一个线程内想更新UI  但是非UI线程是不能更新UI的否则会报错,这时就需要Handler发送消息给主线程去更新UI了。
      

  2.   

    关于为什么需要使用Handler,可以参考一下这个例子
    线程和UI线程的通信package com.proper;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;public class HandlerActivity extends Activity {
        /** Called when the activity is first created. */
     private Button start;
     private ProgressBar bar=null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            System.out.println("点击按钮前"); 
            setContentView(R.layout.main);
            start=(Button)findViewById(R.id.start);
            bar=(ProgressBar)findViewById(R.id.bar);
            start.setText("start");
            start.setOnClickListener(new StartButtonListener());
        }
        class StartButtonListener implements OnClickListener{  public void onClick(View v) {
       bar.setVisibility(View.VISIBLE);
       //将线程对象压进队列,立刻执行Rannable中的run方法。
       handler.post(updateThread);
       // TODO Auto-generated method stub
       System.out.println("--------------1当前线程Id"+Thread.currentThread().getId());
       System.out.println("--------------1当前线程名字"+Thread.currentThread().getName());
      }  
        }
        Handler handler=new Handler(){  @Override
      public void handleMessage(Message msg) {
       // TODO Auto-generated method stub
       System.out.println("--------------3当前线程Id"+Thread.currentThread().getId());
       System.out.println("--------------3当前线程名字"+Thread.currentThread().getName()); 
       bar.setProgress(msg.arg1);
       System.out.println("~~~~~~~~~~~~~~~~~~"+msg.arg1);   //将线程对象压进队列,立刻执行Rannable中的run方法。
       handler.post(updateThread);
       if(msg.arg1==50)
       {
        System.out.println("!!!!!!!!!!!^^^^^!!!!!!!!!!!!");
        handler.removeCallbacks(updateThread);
        System.out.println("****************************");
       }
      }
         
        };
        Runnable updateThread=new Runnable() {
      int i=0;
      public void run() {
       // TODO Auto-generated method stub
       System.out.println("--------------2当前线程Id"+Thread.currentThread().getId());
       System.out.println("--------------2当前线程名字"+Thread.currentThread().getName()); 
      i=i+10;
      //得到一个消息对象
      Message msg=handler.obtainMessage();
      msg.arg1=i;
      try{
       Thread.sleep(1000);
      }
      catch(InterruptedException e){e.printStackTrace();}
      //将消息压进消息队列里面,然后异步执行Handle中的handleMessage方法
      handler.sendMessage(msg);
      }
      
     };
    }<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <ProgressBar 
        android:layout_width="200dp" 
        android:id="@+id/bar" 
        style="?android:attr/progressBarStyleHorizontal" 
        android:visibility="gone" 
        android:layout_height="wrap_content"/>
        <Button
     android:id="@+id/start"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     />
    </LinearLayout>