android开发教程之使用looper处理消息队列
package com.yanjun;
import android.app.activity;
import android.os.bundle;
import android.os.handler;
import android.os.handlerthread;
import android.os.looper;
import android.os.message;
public class handleractivity extends activity {
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
// 通过handlerthread对象来实现使用looper处理消息队列的功能
handlerthread handlerthread = new handlerthread("handlerthread");
// 使线程运行必须调用start()
handlerthread.start();
myhandler myhandler = new myhandler(handlerthread.getlooper());
message message = myhandler.obtainmessage();
// 把message发送到myhandler.obtainmessage()目标处,即谁调用的就发送给谁
bundle bundle = new bundle();
// 通过bundle添加数据
bundle.putint("age", 10);
bundle.putstring("name", "john");
// 将bundle信息添加进去
message.setdata(bundle);
message.sendtotarget();
}
class myhandler extends handler {
public myhandler(looper looper) {
super(looper);
// todo auto-generated constructor stub
}
@override
public void handlemessage(message msg) {
bundle bundle = msg.getdata();
int age = bundle.getint("age");
string name = bundle.getstring("name");
system.out.println("年龄" + age + "名字" + name);
}
}
上一篇: 咳能吃韭菜吗?需要注意哪些?
下一篇: 杏仁美味,那么您知道生杏仁能吃吗?