android Handler Message传递参数,handler子线程和主线程通讯

创建Handler

 

private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch(msg.what){case 1 :   String str1 = msg.getData().getString("text1");//接受msg传递过来的参数String str2 = msg.getData().getString("text2");//接受msg传递过来的参数Log.i("lgq","..ab ==7....11......"+str1);break;}}
};

传递参数

 

Message msg = new Message();
msg.what = 1;
Bundle bundle = new Bundle();
bundle.putString("text1","梅西的消息传递参数的例子!");  //往Bundle中存放数据
bundle.putString("text2","Time:2016-11-03");  //往Bundle中put数据
msg.setData(bundle);//mes利用Bundle传递数据
handler.sendMessage(msg);//用activity中的handler发送消息

 

handler子线程和主线程通讯 

public class HandleActivity extends AppCompatActivity {public static final String TAG = "MainActivity";private Handler mThreadHandler;private Handler mMainHandler = new Handler(new Handler.Callback() {// 主线程接收到子线程发送的消息@Overridepublic boolean handleMessage(Message msg) {if (msg.obj != null) {Log.e(TAG, msg.obj.toString());mThreadReplyText.setText("收到子线程回复:" + msg.obj.toString());}return true;}});private EditText mMainEditText;private TextView mThreadReplyText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.handleactivity);mMainEditText = findViewById(R.id.main_edit_text);mThreadReplyText = findViewById(R.id.thread_reply_text);new Thread(new Runnable() {@Overridepublic void run() {Looper.prepare();mThreadHandler = new Handler(new Handler.Callback() {// 子线程接收到主线程发送的消息并回复@Overridepublic boolean handleMessage(Message msg) {if (msg.obj != null) {Message reply = Message.obtain();reply.obj = "主线程说\"" + msg.obj.toString()+"\"";// 在子线程中使用主线程中的Handler对象的引用向主线程发送消息mMainHandler.sendMessage(reply);}return true;}});Looper.loop();}}).start();}public void onClick(View view) {switch (view.getId()) {case R.id.main_send_button:Message message = Message.obtain();message.obj = mMainEditText.getText();// 主线程向子线程发送消息:在主线程中使用子线程中的Handler对象的引用向子线程发送消息if (mThreadHandler != null) {mThreadHandler.sendMessage(message);} else {while (mThreadHandler == null) {Log.e(TAG, "子线程还没有完成ThreadHandler的创建");if (mThreadHandler != null) {Log.e(TAG, "ThreadHandler创建完成!");mThreadHandler.sendMessage(message);}}}break;}}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415717.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hapi 使用 lab 和 code 进行测试

测试库有很多,我选了lab。 1、安装lab和code yarn add lab code2、创建test文件夹 test文件夹里面的js文件会被跑测试。比如创建index.js。 const { expect } require(code); const Lab require(lab); const lab exports.lab Lab.script();lab.test(returns t…

远程桌面无法连接服务器,启动Terminal Services 服务报1053错误

今天碰到一棘手问题,一上午数据库服务器都无法连接,远程桌面也连不上了,但是可以PING通。项目组的同事一直反映数据库无法连接,耽误工作进度,真急人啊。 跑到机房一看,机器轰轰巨响,进入桌面&a…

【codeforces 534D】Handshakes

【题目链接】:http://codeforces.com/contest/534/problem/D 【题意】 n个人依次进入一个房间; 进进来的人会和房间里面没有组队的人握一次手; (这里的握手只计算主动握手的那个人的握手次数); (任意时刻,任意3个人都能组队); 给出每个人的握手信息; 问你n个…

lab 常用配置参数 代码片段

1、--leaks 启用内存泄漏检测,并在检测到时向您发出警告 2、代码片段 experiment(getting started with hapi testing,, () > {// 这种写法会跳过测试test(lab considers this test as TOOD and skips it)// 这种写法不会跳过测试test(always succeeding :), (…

android 底部表格布局TableLayout

添加依赖compile com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2aar置于底部布局&#xff08;最外层必须是RelativeLayout&#xff09;<?xml version"1.0" encoding"utf-8"?> <RelativeLayout xmlns:android"http://schemas.android.com/a…

Arcgis10安装说明

1、 先安装dotnet framework 3.5 ,必须要装这个才能运行安装程序。 2、 接着安装Arcgis Desktop &#xff0c;只需下一步&#xff0c;下一步执行就好了。完成后&#xff0c;停掉服务。 3、 安装LicenseManager &#xff0c;下一步执行就可以。 4、 接着来破解&…

前端学习(2606):vue简单叙述

第一步 引入 第二步new Vue 第三步 data绑定数据

wux-button 样式扩展 增加shape属性

1、原来的写法和界面图如下 <wux-button block type"orange">拍照确认</wux-button>2、现在的写法只需要增加一个shape"round"参数&#xff0c;可以实现按钮边框为半圆 <wux-button block type"orange" shape"round"&g…

android webView注入js方法

需要在页面加载完成之后才能注入自定义js方法 webView.setWebViewClient(new WebViewClient() {Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) { // view.loadUrl(url);return true;//true时&#xff0c;不可点击跳页效果&…

前端框架Bootstrap简单介绍

下载&#xff1a; 解压之后&#xff1a; 把这些文件拷贝到项目中 页面使用时 只需要引入&#xff1a; 然后我们就可以参考官网来设计需要的前端页面了 设计一个按钮&#xff1a;button 只需要标明css样式中使用的类 下面就是现实效果&#xff1a; 转载于:https://www.cnblo…

CASS软件学习笔记

最近需要学习数据处理&#xff0c;用到的软件是Cass6.1 软件&#xff0c;下面我将学习中的笔记进行了整理。 1、Cass 6.1 支持是CAD2004 &#xff0d;2006&#xff0c;安装的话&#xff0c;先装CAD&#xff0c;然后破解&#xff0c;再装Cass &#xff0c;然后打上补丁&#xf…

vue学习中遇到的错误 Duplicate keys detected: ‘[object Object]‘. This may cause an update error.

前端入门自学&#xff0c;说错的请大神指点。 Avoid using non-primitive value as key, use string/number value instead. Duplicate keys detected: ‘[object Object]’. This may cause an update error.运行后出现这些警告&#xff0c;意思是检测到重复的键值。 把:key里…

mongoose 数据库设计千万要注意 Cast to [number] failed for value

不要定义字段type balance_flow: [{// 提现金额money: { type: Number },// 资金类型 0>提现 withdraw 1>资金流入 user_activetype: Number}],这样会报错

Linux Shell中有三种引号的用法

Linux Shell中有三种引号&#xff0c;分别为双引号&#xff08;" "&#xff09;、单引号( )以及反引号( )。 其中双引号对字符串中出现的$、、和\进行替换&#xff1b;单引号不进行替换&#xff0c;将字符串中所有字符作为普通字符输出&#xff0c;而反引号中字符串作…

工作113:添加echart折线图

<template><div><el-card><div slot"header" class"clearfix"><span>订单信息</span></div><!--样式控制全部 已经退回 待审核--><el-row><el-radio-group v-model"query.status" siz…

测试管理

测试管理工作检查表&#xff1a; 1. 检查每轮测试开始时测试环境是否准备好&#xff08;包括软件硬件、测试基本数据等&#xff09;&#xff1b; 2. 确保测试环境&#xff08;数据和程序&#xff09;与开发分离&#xff0c;除了测试组之外其他人不能更新测试环境的数据和程序…

android 获取webView高度,设置webView高度

1、注入获取webView高度的js方法 webView.setWebViewClient(new WebViewClient() {Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) { // view.loadUrl(url);return true;//true时&#xff0c;不可点击跳页效果&#xff0c;false时…

如何设计一个高并发系统

如何设计一个高并发系统&#xff1f; 如果你确实有真才实学&#xff0c;在互联网公司里&#xff0c;干过高并发系统&#xff0c;那你拿Offer&#xff0c;基本如探囊取物一样简单。 但你要真干过高并发系统&#xff0c;面试官绝对不会问这个问题&#xff0c;否则他就不太明智了…

李超线段树 [Heoi2013]Segment

问题 D: [Heoi2013]Segment 时间限制: 4 Sec 内存限制: 256 MB 题目描述 要求在平面直角坐标系下维护两个操作&#xff1a; 1.在平面上加入一条线段。记第i条被插入的线段的标号为i。 2.给定一个数k,询问与直线 x k相交的线段中&#xff0c;交点最靠上的线段的编号。 输…