handle 与 Message

 1 package com.douaty.asynctaskdemo;
 2
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.os.Handler;
 6 import android.os.Message;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10
11
12 public class MessageActivity extends Activity {
13
14     int messageId = 0x1000;
15     TextView tv_connet;
16     Button bt_send;
17     Button bt_post;
18
19     private Handler handler = new Handler() {
20         @Override
21         public void handleMessage(Message msg){
22             if (msg.what == messageId){
23                 tv_connet.setText((String)msg.obj);
24                 System.out.println("--arg1: " + msg.arg1 + ",  arg2: " + msg.arg2);
25             }
26         }
27     };
28
29     @Override
30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.activity_message);
33
34         tv_connet = (TextView) findViewById(R.id.tv_showcontnet);
35         bt_send = (Button) findViewById(R.id.bt_send);
36         bt_post = (Button) findViewById(R.id.bt_post);
37
38         bt_send.setOnClickListener(new View.OnClickListener() {
39             @Override
40             public void onClick(View v) {
41                 new Thread(new Mythread()).start();
42             }
43         });
44
45         bt_post.setOnClickListener(new View.OnClickListener() {
46             @Override
47             public void onClick(View v) {
48                 handler.post(new Runnable() {
49                     @Override
50                     public void run() {
51                         Message msg = Message.obtain(handler, messageId, 123, 456, "hello");
52                         msg.sendToTarget();
53                     }
54                 });
55             }
56         });
57     }
58
59     public class Mythread implements Runnable {
60
61         @Override
62         public void run() {
63             try {
64                 Thread.sleep(1000);
65             } catch (InterruptedException e) {
66                 e.printStackTrace();
67             }
68 //            //方式1
69 //            Message msg = Message.obtain();
70 //            msg.obj = "我是一个Handler!";
71 //            msg.what = messageId;
72 //            handler.sendMessage(msg);
73 //            //方式2
74 //            Message msg = Message.obtain(handler);
75 //            msg.obj = "我是一个Handler!";
76 //            msg.what = messageId;
77 //            msg.sendToTarget();
78 //            //方式3
79 //            Message msg = Message.obtain(handler, messageId);
80 //            msg.obj = "我是一个Handler!";
81 //            msg.sendToTarget();
82 //            //方式4
83 //            Message msg = Message.obtain(handler, messageId, "我是一个Handler!");
84 //            msg.sendToTarget();
85             //方式5
86             Message msg = Message.obtain(handler, messageId, 123, 456, "hello");
87             msg.sendToTarget();
88         }
89     }
90 }  
时间: 2024-08-02 22:22:33

handle 与 Message的相关文章

You Probably Don’t Need a Message Queue

原文地址 I’m a minimalist, and I don’t like to complicate software too early and unnecessarily. And adding components to a software system is one of the things that adds a significant amount of complexity. So let’s talk about message queues. Message Queu

安卓Handle的深入剖析和使用

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;">在公司开发项目你不能说handle用的不多,反正这种更新主线程的机制是必须要懂的.面试的时候也总会叫你回答handle.looper.MessageQueen和Message的区别,所以你不仅仅只

Meandering Through the Maze of MFC Message and Command Routing

Meandering Through the Maze of MFC Message and Command Routing Paul DiLascia Paul DiLascia is a freelance software consultant specializing in developing C++ applications for Windows. He is the author of Windows++: Writing Reusable Code in C++ (Addiso

Caliburn.Micro学习笔记(四)----IHandle&lt;T&gt;实现多语言功能

Caliburn.Micro学习笔记(四)----IHandle<T>实现多语言功能 说一下IHandle<T>实现多语言功能 因为Caliburn.Micro是基于MvvM的UI与codebehind分离, binding可以是双向的所以我们想动态的实现多语言切换很是方便今天我做一个小demo给大家提供一个思路 先看一下效果 点击英文  变成英文状态点chinese就会变成中文                          源码的下载地址在文章的最下边 多语言用的是资源文件建

Caliburn.Micro学习笔记(三)----事件聚合IEventAggregator和 Ihandle&lt;T&gt;

Caliburn.Micro学习笔记(三)----事件聚合IEventAggregator和 Ihandle<T> 今天 说一下Caliburn.Micro的IEventAggregator和IHandle<T>分成两篇去讲这一篇写一个简单的例子 看一它的的实现和源码 下一篇用它们做一个多语言的demo 这两个是事件的订阅和广播,很强大,但用的时候要小心发生不必要的冲突. 先看一下它的实现思想 在Caliburn.Micro里EventAggregator要以单例的形式出现这样可以

TControlStyle.csParentBackground的作用(附Delphi里的所有例子,待续)

Only applicable when Themes are enabled in applications on Windows XP. Causes the parent to draw its background into the control's background. This is useful for controls that need to show their parent's theme elements, such as aTPanel or TFrame that

The File&#39;s Owner

The File Owner is an instantiated, runtime object that owns the contents of your nib and its outlets/actions when the nib is loaded. It can be an instance of any class you like - take a look at the identity tab of the tool palette. For example, consi

GDI与GDI+性能比较

编写程序对GDI和GDI+绘制进行了比较,经过比较,GDI相对GDI+还是有一些性能优势的. 同时比较了每次绘制创建TGPGraphics对象和共用一个TGPGraphics对象的情况,两者性能相差不大,几可忽略. 1.用GDI绘制5K次----耗时约为19s200ms procedure TForm8.WMPaint(var Message: TWMPaint); var ps: PAINTSTRUCT; LClientRect: TGPRect; LGraph: TGPGraphics; L

[转]Skynet之斗转星移 - 将控制权交给Lua

Skynet之斗转星移 - 将控制权交给Lua http://www.outsky.org/code/skynet-lua.html Sep 7, 2014 在我看来,Skynet的一个重要优势是与Lua的高度结合,完全可以用Lua写服务.用C写服务的原理很简单:通过动态链接库的形式,提供create.init和release接口,供主进程在需要的时候载入服务,并将处理消息的回调函数一并注入主进程,这样,当主进程给此服务发消息时,消息就进入此回调函数处理. 由此可见,服务最重要的部分就是这个回调