线程处理UI的理解Demo

这个例子对于子线程不能处理UI线程的View可以有助于了解:

package com.example;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
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;
import android.widget.LinearLayout;
import android.widget.TextView;

/*介紹過Android主線程與子線程之溝通。所謂主線程通常是UI線程。
* Android的UI是單線程(Single-threaded)的。為了避免拖住GUI,
* 一些較費時的物件應該交給獨立的線程去執行。如果幕後的線程來執行UI物件,
* Android就會發出錯誤訊息CalledFromWrongThreadException
* 基本上,Android希望UI thread能夠給予User做快速的反應。
* 如果UI thread花費太多時間做幕後的事情,超過5秒鐘,Android就會給user提示
*/

public class Looper_6 extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
public TextView tv;
private myThread t;
private Button btn, btn2;
private Handler h;
private Context ctx;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ctx = this;
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

btn = new Button(this);
btn.setId(101);
btn.setBackgroundResource(R.drawable.icon);
btn.setText("test looper");
btn.setOnClickListener(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 50);
param.topMargin = 10;
layout.addView(btn, param);

btn2 = new Button(this);
btn2.setId(102);
btn2.setBackgroundResource(R.drawable.icon);
btn2.setText("exit");
btn2.setOnClickListener(this);
layout.addView(btn2, param);

tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setText("");
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
layout.addView(tv, param2);
setContentView(layout);
// ------------------------
t = new myThread();
t.start();
}

public void onClick(View v) {
switch (v.getId()) {
case 101:
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String obj = "mainThread";
Message m = h.obtainMessage(1, 1, 1, obj);
h.sendMessage(m);
break;
case 102:
h.getLooper().quit();
finish();
break;
}
}

public class EventHandler extends Handler {
public EventHandler(Looper looper) {
super(looper);
}

@Override
public void handleMessage(Message msg) {
tv.setText((String) msg.obj);
}
}

class myThread extends Thread {
final boolean TEST_FLAG = false;

public void run() {
Looper.prepare();
h = new Handler() {
public void handleMessage(Message msg) {
if (TEST_FLAG)
// tv物件是主線程誕生的UI物件
// 如果子線程也去插手的話,Android程式就停止了
tv.setText("myThread is running");
else {
EventHandler ha = new EventHandler(Looper
.getMainLooper());
String obj = (String) msg.obj + ", myThread";
Message m = ha.obtainMessage(1, 1, 1, obj);
ha.sendMessage(m);
}
}
};
Looper.loop();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值