1.Handler默认情况下与主线程处于同一线程
public class MainActivity extends Activity { private Button startButton = null; private Button endButton = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startButton = (Button) findViewById(R.id.startButton); endButton = (Button) findViewById(R.id.endButton); startButton.setOnClickListener(new StartButtonListener()); endButton.setOnClickListener(new EndButtonListener()); /* * if (savedInstanceState == null) { * getSupportFragmentManager().beginTransaction() .add(R.id.container, * new PlaceholderFragment()).commit(); } */ } class StartButtonListener implements OnClickListener { @Override public void onClick(View v) { System.out.println(Thread.currentThread().getId() + " Start"); handler.post(updateThread); } } class EndButtonListener implements OnClickListener { @Override public void onClick(View v) { System.out.println(Thread.currentThread().getId() + " End"); handler.removeCallbacks(updateThread); } } Handler handler = new Handler(); Runnable updateThread = new Runnable() { @Override public void run() { System.out .println(Thread.currentThread().getId() + " UpdateThread"); handler.postDelayed(updateThread, 3000); } };
2.通过Handler实现进度条
public class MainActivity extends Activity { ProgressBar bar = null; Button startButton = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println("主线程ID:"+Thread.currentThread().getId()); bar = (ProgressBar) findViewById(R.id.bar); startButton = (Button) findViewById(R.id.startButton); startButton.setOnClickListener(new ButtonListener()); /* * if (savedInstanceState == null) { * getSupportFragmentManager().beginTransaction() .add(R.id.container, * new PlaceholderFragment()).commit(); } */ } class ButtonListener implements OnClickListener { @Override public void onClick(View v) { bar.setVisibility(View.VISIBLE); updateBarHandler.post(updateThread); } } //使用匿名内部类 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"); System.out.println("Handler线程ID:"+Thread.currentThread().getId()); i = i + 10; //得到一个消息,Message类是有Android操作系统提供的 Message msg = updateBarHandler.obtainMessage(); //将msg对象的arg1参数的值设置为i,用arg1和arg2这两个 //成员变量传递值,优点是消耗性能小 msg.arg1 = i; try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } updateBarHandler.sendMessage(msg); if (i >= 100) { updateBarHandler.removeCallbacks(updateThread); } } };
3.Handler通过post执行Runnable(还是与主线程处于同一线程)
public class MainActivity extends Activity { private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //handler线程和主线程在同一线程 handler.post(r); /*Thread t=new Thread(r); t.start();*/ System.out.println("activity-->" + Thread.currentThread().getId()); System.out.println("activityname-->" + Thread.currentThread().getId()); /* * if (savedInstanceState == null) { * getSupportFragmentManager().beginTransaction() .add(R.id.container, * new PlaceholderFragment()).commit(); } */ } Runnable r = new Runnable() { @Override public void run() { System.out.println("handler-->" + Thread.currentThread().getId()); System.out.println("handlername-->" + Thread.currentThread().getName()); try { Thread.sleep(10000); } catch (Exception e) { e.printStackTrace(); } } };
4.通过HandlerThread执行线程,并可通过Bundle传递参数
public class MainActivity02 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity02); System.out.println(Thread.currentThread().getId()); //生成一个HandlerThread对象,实现了使用Looper来处理消息队列的功能,这个类由Android提供 HandlerThread handlerThread=new HandlerThread("handler_Thread"); handlerThread.start(); MyHandler myHandler=new MyHandler(handlerThread.getLooper()); Message msg=myHandler.obtainMessage(); msg.obj="abc";//通过obj存储数据 Bundle b=new Bundle(); b.putInt("age", 20); b.putString("name", "Jhon"); msg.setData(b);//通过Bundle存储数据 msg.sendToTarget();//将msg发送到目标对象,所谓的目标对象就是生成该msg的Handler /*if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); }*/ } class MyHandler extends Handler{ public MyHandler(){ } public MyHandler(Looper looper){ super(looper); } @Override public void handleMessage(Message msg){ System.out.println(msg.obj); Bundle b=msg.getData(); System.out.println(b.getInt("age")); System.out.println(b.getString("name")); System.out.println("handleMessage:"+Thread.currentThread().getId()); } }