歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android-初識Handler-子線程異步更新UI

Android-初識Handler-子線程異步更新UI

日期:2017/3/1 10:56:28   编辑:Linux編程

簡單的例子:子線程異步的更新UI,同時不影響主線程的操作本身界面的操作
下面是main.xml配置文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <Button
  7. android:id="@+id/btnFirst"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="按鈕1" >
  11. </Button>
  12. <Button
  13. android:id="@+id/btnSecond"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text="按鈕2" >
  17. </Button>
  18. </LinearLayout>

代碼:

  1. package com.liujin.game;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Looper;
  6. import android.os.Message;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. public class TestHandlerImage extends Activity implements OnClickListener {
  11. private Button btnFirst, btnSecond;
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. btnFirst = (Button) this.findViewById(R.id.btnFirst);//得到按鈕
  16. btnSecond = (Button) this.findViewById(R.id.btnSecond);
  17. btnFirst.setOnClickListener(this);//設置監控
  18. btnSecond.setOnClickListener(this);
  19. MyThread myThread = new MyThread();//開啟子線程
  20. myThread.start();
  21. }
  22. @Override
  23. public void onClick(View view) {
  24. switch (view.getId()) {
  25. case R.id.btnFirst:
  26. btnFirst.setText("我點擊按鈕1");
  27. btnSecond.setText("等待點擊");
  28. break;
  29. case R.id.btnSecond:
  30. btnFirst.setText("等待點擊");
  31. btnSecond.setText("我點擊按鈕2");
  32. break;
  33. }
  34. }
  35. class MyThread extends Thread {
  36. TitleEventHandler handler;
  37. public int sleepTime=1000;
  38. public int Interval=0;
  39. public void run() {
  40. Looper mainLooper = Looper.getMainLooper();//得到主線程的Looper,每一個Activity都默認自帶一個Looper看上一篇的源碼
  41. handler = new TitleEventHandler(mainLooper);
  42. handler.removeMessages(0);
  43. Message msg = null;
  44. Long start,end;
  45. int time = 15;
  46. while (true) {//下面的代碼很簡單就是定時的每過一秒發送一個消息給主線程
  47. try {
  48. time--;
  49. start=System.currentTimeMillis();
  50. msg = handler.obtainMessage(1, 1, 1, "當前還剩" + (time + 1)+ "秒");
  51. handler.sendMessage(msg);//發送消息
  52. end=System.currentTimeMillis();
  53. Interval=(int) (end-start);
  54. if(Interval<sleepTime){//定時睡覺疫苗
  55. Thread.sleep(sleepTime-Interval);
  56. }
  57. } catch (InterruptedException e) {
  58. }
  59. }
  60. }
  61. }
  62. class TitleEventHandler extends Handler {
  63. public TitleEventHandler() {
  64. super();
  65. }
  66. public TitleEventHandler(Looper looper) {
  67. super(looper);
  68. }
  69. @Override
  70. public void handleMessage(Message msg) {
  71. super.handleMessage(msg);
  72. setTitle((String) msg.obj);
  73. }
  74. }
  75. }

下面是main.xml配置文件

Copyright © Linux教程網 All Rights Reserved