handle 与 Message
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于handle 与 Message的主要内容,如果未能解决你的问题,请参考以下文章
Android Message Handling Mechanism
!MESSAGE Error occurred during status handling
Android Message handling (based on KK4.4)
Selenium Xpath元素无法定位 NoSuchElementException: Message: no such element: Unable to locate element(代码片段