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

來源:互聯網
上載者:User

 

簡單的例子:子線程非同步更新UI,同時不影響主線程的操作本身介面的操作
 下面是main.xml設定檔

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/btnFirst"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="按鈕1" >    </Button>    <Button        android:id="@+id/btnSecond"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="按鈕2" >    </Button></LinearLayout>

代碼: 

package com.liujin.game;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Looper;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class TestHandlerImage extends Activity implements OnClickListener {private Button btnFirst, btnSecond;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btnFirst = (Button) this.findViewById(R.id.btnFirst);//得到按鈕btnSecond = (Button) this.findViewById(R.id.btnSecond);btnFirst.setOnClickListener(this);//設定監控btnSecond.setOnClickListener(this);MyThread myThread = new MyThread();//開啟子線程myThread.start();}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.btnFirst:btnFirst.setText("我點擊按鈕1");btnSecond.setText("等待點擊");break;case R.id.btnSecond:btnFirst.setText("等待點擊");btnSecond.setText("我點擊按鈕2");break;}}class MyThread extends Thread {TitleEventHandler handler;public int sleepTime=1000;public int Interval=0;public void run() {Looper mainLooper = Looper.getMainLooper();//得到主線程的Looper,每一個Activity都預設內建一個Looper看上一篇的源碼handler = new TitleEventHandler(mainLooper);handler.removeMessages(0);Message msg = null;Long start,end;int time = 15;while (true) {//下面的代碼很簡單就是定時的每過一秒發送一個訊息給主線程try {time--;start=System.currentTimeMillis();msg = handler.obtainMessage(1, 1, 1, "當前還剩" + (time + 1)+ "秒");handler.sendMessage(msg);//發送訊息end=System.currentTimeMillis();Interval=(int) (end-start);if(Interval<sleepTime){//定時睡覺疫苗Thread.sleep(sleepTime-Interval);}} catch (InterruptedException e) {}}}}class TitleEventHandler extends Handler {public TitleEventHandler() {super();}public TitleEventHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);setTitle((String) msg.obj);}}} 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.