消息通道looper
2016-05-28 16:14
381 查看
FirstAcitvitypublic class FirstActivity extends AppCompatActivity {
private Button but = null;
private static final int SET = 1; //what操作码
private TextView info = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.info = (TextView)findViewById(R.id.info);
this.but = (Button)findViewById(R.id.but);
this.but.setOnClickListener(new OnClickListenterImpl());
}
private class OnClickListenterImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.but:
Looper looper = Looper.myLooper(); //取得当前线程
MyHandler myHandler = new MyHandler(looper); //构造一个handler
myHandler.removeMessages(0); //清空所有的消息队列
String data = "hello,world";
Message msg = myHandler.obtainMessage(SET,1,1,data); //发送消息
myHandler.sendMessage(msg);
break;
}
}
}
private class MyHandler extends android.os.Handler {
public MyHandler(Looper looper) { //接收handler
super(looper); //调用父类构造
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
FirstActivity.this.info.setText(msg.obj.toString());
}
}
}
}
2. activity_main.xml<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/info"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="启动"
android:layout_below="@+id/info"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="76dp" />
private Button but = null;
private static final int SET = 1; //what操作码
private TextView info = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.info = (TextView)findViewById(R.id.info);
this.but = (Button)findViewById(R.id.but);
this.but.setOnClickListener(new OnClickListenterImpl());
}
private class OnClickListenterImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.but:
Looper looper = Looper.myLooper(); //取得当前线程
MyHandler myHandler = new MyHandler(looper); //构造一个handler
myHandler.removeMessages(0); //清空所有的消息队列
String data = "hello,world";
Message msg = myHandler.obtainMessage(SET,1,1,data); //发送消息
myHandler.sendMessage(msg);
break;
}
}
}
private class MyHandler extends android.os.Handler {
public MyHandler(Looper looper) { //接收handler
super(looper); //调用父类构造
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
FirstActivity.this.info.setText(msg.obj.toString());
}
}
}
}
2. activity_main.xml<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/info"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="启动"
android:layout_below="@+id/info"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="76dp" />
相关文章推荐
- Hadoop数据读写原理
- 查看CentOS系统版本是32位的还是64位的
- Systemd 进程管理相关
- cloudstack on centos6.5 笔记
- linux运维人员必会开源运维工具体系
- Maven配置一键部署远程Tomcat
- 搭建hadoop平台的细节点与注意点
- Class org.apache.struts2.json.JSONWriter can not access a member of
- Copy List with Random Pointer
- 服务器之Windows和Linux
- Docker在PHP项目开发环境中的应用
- POP3、SMTP和IMAP之间的区别和联系
- 网站系统开发参考网址
- 嵌入式linux init系统(busybox init)
- phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)
- phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)
- shell基础知识
- HA高可用
- AOP 实现的原理简析
- 搭建windows(win7)平台hadoop开发环境