根据我的理解大概是这样
handler其实相当于一个监听器,这个监听器可以向线程的消息队列发送消息,looper相当于消息队列的监听器,它一直在监听消息队列,当有消息到达时,调用handler的handlemessage方法来处理.
关于handlerthread,为啥要用这个?
免得你在主线程里启动n个线程,另外使用handlerthread的looper可以降低mainlooper的压力,猜的呵呵.
mainactivity.java:
package com.ydd.handler;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button start = null;
private ProgressBar progressBar = null;
private Handler UIhandler= null;
private Handler childhandler= null;
private HandlerThread handlerThread = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
handlerThread = new HandlerThread("testThread");
handlerThread.start();
childhandler = new Handler(handlerThread.getLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(Message arg0) {
Message message = UIhandler.obtainMessage();
System.out.println("handleMessage in "+Thread.currentThread().getName());
message.arg1 = arg0.what;
UIhandler.sendMessage(message);
return false;
}
});
UIhandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
System.out.println("handleMessage in "+Thread.currentThread().getName());
progressBar.setProgress(msg.arg1);
}
};
start.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
progressBar.setVisibility(ProgressBar.VISIBLE);
for(int i=1;i<=10;i++){
childhandler.sendEmptyMessageDelayed(10*i, 1000*i);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ydd.handlerprogressbar.MainActivity" >
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
/>
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Start"
/>
</LinearLayout>