这个例子也是Demo4的另外一种实现方式,大家可以对比学习:
Java代码
package com.example.Looper_05;

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;

public class Looper_05 extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
public TextView tv;
private RR r;
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);
// ------------------------
r = new RR();
}

public void onClick(View v) {
switch (v.getId()) {
case 101:
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) {
((Activity) ctx).setTitle((String) msg.obj);
}
}

// ------------------------------------------------
public class RR implements Runnable {
public RR() {
Thread aThread = new Thread(null, this, "RR");
aThread.start();
}

public void run() {
Looper.prepare();
h = new Handler() {
public void handleMessage(Message msg) {
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();
}
}
}

更多相关文章

  1. Android(安卓)远程图片获取和本地缓存(三)
  2. android TextView 垂直自动滚动字幕实现
  3. Android开发便签3:TextView的自动检测文本中的超链接和自动补全
  4. Android源码修改后的语音录音代码
  5. webkit中的javascript和android交互
  6. SQLiteException: no such column:好
  7. Android(安卓)ApiDemos示例解析(166):Views->Layouts->TableLayo
  8. android中引入okhttp

随机推荐

  1. Android需要大量内存时自己设置堆大小
  2. 第一个Android程序
  3. php实现post请求 接收android客户端json
  4. Android开发---xml布局文件中的常见属性
  5. 大厂面试秘籍—— 深入理解 Handler
  6. 《Android内核剖析》读书笔记 第18章 And
  7. Android(安卓)操作串口 (android serial
  8. Android实践之简易天气(二)
  9. Android(安卓)图像绘制之 Drawable
  10. Android系统定制和源码开发以及源码编译(