android handler
handler线程与UI(activity)线程为同一线程
简单用法
<?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" > <Button android:id="@ id/startButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start" /> <Button android:id="@ id/endButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="end"/> </LinearLayout>
public class HandlerActivity extends Activity { /** Called when the activity is first created. */ //声明两个按钮控件 private Button startButton = null; private Button endButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根据控件的ID得到代表控件的对象,并未这两个按钮设置相应的监听器 startButton = (Button)findViewById(R.id.startButton); startButton.setOnClickListener(new StartButtonListener()); endButton = (Button)findViewById(R.id.endButton); endButton.setOnClickListener(new EndButtonListener()); } class StartButtonListener implements OnClickListener{ @Override public void onClick(View v) { //调用Handler的post方法,将要执行的线程对象添加到队列当中 handler.post(updateThread); } } class EndButtonListener implements OnClickListener{ @Override public void onClick(View v) { //移除消息队列 handler.removeCallbacks(updateThread); } } //创建一个Handler对象 Handler handler = new Handler(); //将要执行的操作写在线程对象的run方法当中 Runnable updateThread = new Runnable(){ @Override public void run() { System.out.println("UpdateThread"); //在run方法内部,执行postDelayed或者是post方法 handler.postDelayed(updateThread, 3000); } }; }
添加消息队列
public class TestBarHandler extends Activity { /** Called when the activity is first created. */ //声明控件变量 ProgressBar bar = null; Button startButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根据控件的ID得到代表控件的对象,并为按钮设置监听器 bar = (ProgressBar)findViewById(R.id.bar); startButton = (Button)findViewById(R.id.startButton); startButton.setOnClickListener(new ButtonListener()); } //当点击startButton按钮时,就会执行ButtonListener的onClick方法 class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub bar.setVisibility(View.VISIBLE); updateBarHandler.post(updateThread); } } //使用匿名内部类来复写Handler当中的handleMessage方法 Handler updateBarHandler = new Handler(){ @Override public void handleMessage(Message msg) { bar.setProgress(msg.arg1); updateBarHandler.post(updateThread); } }; //线程类,该类使用匿名内部类的方式进行声明 Runnable updateThread = new Runnable(){ int i = 0 ; @Override public void run() { System.out.println("Begin Thread"); i = i+10 ; //得到一个消息对象,Message类是有Android操作系统提供 Message msg = updateBarHandler.obtainMessage(); //将msg对象的arg1参数的值设置为i,用arg1和arg2这两个成员变量传递消息,优点是系统性能消耗较少 msg.arg1 = i ; try { //设置当前显示睡眠1秒 Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //将msg对象加入到消息队列当中 updateBarHandler.sendMessage(msg); if( i == 100){ //如果当i的值为100时,就将线程对象从handler当中移除 updateBarHandler.removeCallbacks(updateThread); } } }; }
<?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:id="@ id/bar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" /> <Button android:id="@ id/startButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="start"/> </LinearLayout>
通过打印线程ID,handler与ui线程为同一线程,会产生阻塞
如果不想产生阻塞需通过Thread产生新的线程
public class HandlerTest extends Activity { private Handler handler = new Handler(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // sendMessage(); //handler.post(r); setContentView(R.layout.main); Thread t = new Thread(r); t.start(); System.out.println("activity--->" Thread.currentThread().getId()); System.out.println("activityname--->" Thread.currentThread().getName()); } Runnable r = new Runnable(){ @Override public void run() { // TODO Auto-generated method stub System.out.println("handler--->" Thread.currentThread().getId()); System.out.println("handlername--->" Thread.currentThread().getName()); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }
通过HandlerThread创建一个与ui不同线程的Handler
Message通过Bundle传递信息
public class HandlerTest2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); //打印了当前线程的ID System.out.println("Activity-->" Thread.currentThread().getId()); //生成一个HandlerThread对象,实现了使用Looper来处理消息队列的功能,这个类由Android应用程序框架提供 HandlerThread handlerThread = new HandlerThread("handler_thread"); //在使用HandlerThread的getLooper()方法之前,必须先调用该类的start(); handlerThread.start(); MyHandler myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); //将msg发送到目标对象,所谓的目标对象,就是生成该msg对象的handler对象 Bundle b = new Bundle(); b.putInt("age", 20); b.putString("name", "Jhon"); msg.setData(b); msg.sendToTarget(); } class MyHandler extends Handler{ public MyHandler(){ }
//handler绑定在另外一个线程 public MyHandler(Looper looper){ super(looper); } @Override public void handleMessage(Message msg) { Bundle b = msg.getData(); int age = b.getInt("age"); String name = b.getString("name"); System.out.println("age is " age ", name is" name); System.out.println("Handler--->" Thread.currentThread().getId()); System.out.println("handlerMessage"); } } }