Handler

一、             Handler

Activity代码:

package org.demo.handler;

 

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 HandlerDemo extends Activity {

       private Button btn1;

       private Button btn2;

       private ProgressBar bar=null;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        btn1=(Button)findViewById(R.id.btn1);

        btn2=(Button)findViewById(R.id.btn2);

        btn1.setOnClickListener(new Start());

        btn2.setOnClickListener(new End());

        bar=(ProgressBar)findViewById(R.id.bar1);

}

//声明一个Handler对象

Handler handler=new Handler(){

//覆写handleMessage方法

              @Override

              public void handleMessage(Message msg) {

                     // TODO Auto-generated method stub

                     bar.setProgress(msg.arg1);

                     handler.post(r);

                     super.handleMessage(msg);

              }         

    };

    class Start implements OnClickListener{

 

              public void onClick(View v) {

                     // TODO Auto-generated method stub

                     bar.setProgress(0);

                     bar.setVisibility(ProgressBar.VISIBLE);

            //调用handler的post的方法发送线程类

                     handler.post(r);

              }

           

    }

   

    class End implements OnClickListener{

 

              public void onClick(View v) {

                     // TODO Auto-generated method stub

                     handler.removeCallbacks(r);

              }

           

}

//构造一个线程类

    Runnable r=new Runnable(){

           int i=0;

              public void run() {

                      i=i+10;

             //构造Message对象

                      Message msg=handler.obtainMessage();

                      msg.arg1=i;

                      try{

                             Thread.sleep(3000);

                      }catch(Exception e){

                             e.printStackTrace();

                      }

            //调用handler的sendMessage发送构造的message对象

                      handler.sendMessage(msg);

                      if(i==100){

                             handler.removeCallbacks(r);

                             

                             bar.setVisibility(ProgressBar.GONE);                       

                      }

              }

           

    };

   

}

二、Handler中使用Looper:

package org.demo.handlerdemo2;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.HandlerThread;

import android.os.Looper;

import android.os.Message;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

 

public class HandlerDemo2 extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button btn=(Button)findViewById(R.id.btn);

        btn.setOnClickListener(new OnClickListener(){

                     public void onClick(View v) {

                            //使用HandlerThread对象构造

                             HandlerThread handler=new HandlerThread("t1");

                             handler.start();

               //使用Looper处理消息队列

                             MyHandler myhandler=new MyHandler(handler.getLooper());

                             Message msg=myhandler.obtainMessage();

                             Bundle b=new Bundle();

                             b.putString("name", "百度"+System.currentTimeMillis());

                             msg.setData(b);

                             //发送数据

msg.sendToTarget();

                     }

               

        });

      

    }

    class MyHandler extends Handler{

           public MyHandler(){

                  

           }

           public MyHandler(Looper loop){

                  super(loop);

           }

              @Override

              public void handleMessage(Message msg) {

                     // TODO Auto-generated method stub

            //接受数据并处理

                     Bundle b=msg.getData();

                     String s=b.getString("name");

                     Toast.makeText(HandlerDemo2.this, s+"--"+Thread.currentThread().getName(), Toast.LENGTH_LONG).show();

                     super.handleMessage(msg);

              }

           

    }

}

 

posted @ 2011-08-29 22:57  rorshach  阅读(128)  评论(0编辑  收藏  举报