Bundle主要用于传递数据;它保存的数据,是以key-value(键值对)的形式存在的。

Bundle经常使用在Activity之间或者线程间传递数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。

当Bundle传递的是对象或对象数组时,必须实现Serializable 或Parcelable接口。

Bundle提供了各种常用类型的putXxx()/getXxx()方法,用于读写基本类型的数据。(各种方法可以查看API)

在activity间传递信息

Intent intent  = new Intent(mContext,DetailActivity.class);
Bundle bundle = new Bundle(); bundle.putCharSequence("newsId",newsId);
bundle.putString("sff", "value值"); //key-"sff",通过key得到value-"value值"(String型)
bundle.putInt("iff", ); //key-"iff",value-175 intent.putExtras(bundle); //通过intent将bundle传到另个Activity
startActivity(intent);

接收数据

 //接收Intent发过来的数据
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String newsId = bundle.getString("newsId");

线程间传递

通过Handler将带有dundle数据的message放入消息队列,其他线程就可以从队列中得到数据

Message message=new Message();//new一个Message对象
message.what = MESSAGE_WHAT_2;//给消息做标记
Bundle bundle = new Bundle(); //得到Bundle对象
bundle.putString("text1","消息传递参数的例子!"); //往Bundle中存放数据
bundle.putInt("text2",); //往Bundle中put数据
message.setData(bundle);//mes利用Bundle传递数据
mHandler.sendMessage(message);//Handler将消息放入消息队列

读取数据
这里用的是Handler的handleMessage(Message msg)方法处理数据

String str1=msg.getData().getString("text1");
int int1=msg.getData().getString("text2");

转自:https://blog.csdn.net/yiranruyuan/article/details/78049219

Bundle传递数据,Handler更新UI的更多相关文章

  1. Android中多线程编程(三)Handler更新UI的方式

    Handler更新UI的方式和原因以及遇到的问题 1.方式: 仅仅能通过Handler来更新UI. 代码例如以下: package com.chengdong.su.handlerdemo; impo ...

  2. Android Handler传递参数动态更新UI界面demo

    package com.example.demo_test; import android.app.Activity; import android.os.Bundle; import android ...

  3. android Handler更新UI

    android中经常需要更新界面某个元素的值,但是在主线程中是不可以直接更新主线程的值.这里推荐通过handler机制来更新值. 一Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主 ...

  4. handler更新ui线程的基本用法

    1.因为费时操作要放子线程,更新UI要放UI线程(主线程),所以子线程和主线程通信,通信的话要用到handler这个东西. 这里讲的比较简单,举2个例子说明 2.使用post的是handler ,使用 ...

  5. Android Bundle传递数据

    1.传递普通数据 Intent intent=new Intent(MainActivity.this,TwoActivity.class); Bundle bundle=new Bundle(); ...

  6. WPF 修改数据后更新UI

    ObservableCollection<T> 只有项添加或删除才会更新UI 要想属性发生变动后立刻更新到UI,必须继承 INotifyPropertyChanged 接口,示例如下 pu ...

  7. Activity通过bundle传递数据

    从AActivity.java向BActivity.java传递数据: 建立AActivity.java文件建立bundle: 1 public class AActivity extends App ...

  8. handler更新UI主线程

    示例:下面代码的功能是修改UI主线程TextView的内容 public class MainActivity extends Activity { private Button btn_start; ...

  9. Android笔记——Handler更新UI示例

    public class MainActivity extends ActionBarActivity { private TextView textView; private int i=0; @O ...

随机推荐

  1. Android - Telephony API 1.6

    SignalStrength: 1. public int getGsmSignalStrength() : GSM Signal Strength, valid values are (0-31, ...

  2. grunt 常用插件

    grunt-contrib-uglify:代码压缩 grunt-contrib-jshint:检查js拼写错误 csslint:检查css语法错误

  3. Linux中一些 不是很常用的配置修改

    1,让虚拟机屏幕最大化 :查看-->自动调整大小-->自动适应客户机 2,让虚拟机取消屏保: system --> preferences --> Screensaver

  4. 挂载U盘到linux中

        一.  挂载U盘到linux中,也可以是虚拟机中的linux 1. 首先插上U盘 2. fdisk -l 找到自己的U盘设备,并且记住文件系统类型,主要看空间大小来判断,比如是/dev/sdc ...

  5. 把序列中的N个元素赋值给多个变量

    说明: python中序列和散列 序列:可通过偏移量来进行切片的对象.列表.元组.字符串都属于序列. 散列:无法通过偏移量来进行切片的对象.比如 集合.字典 一. 变量的数量跟序列元素的个数一样 1. ...

  6. Nginx文件上传下载实现与文件管理

    1.Nginx 上传 Nginx 依赖包下载 # wget http://www.nginx.org/download/nginx-1.2.2.tar.gzinx # wget http://www. ...

  7. MySQL , MHA , Haproxy 配置

    1. 基本架构 2. 读端口影射 3. 写端口影射 进行一个Health Check MHA Manager对Master节点MySQL 进行存活监控 读FailOver 1 读FailOver 2 ...

  8. smtp自动发送邮件demo

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  9. jedis 整合 Spring

    一,   单机版整合spring 1. 使用默认配置文件进行配置 <bean id="redisClient" class="redis.clients.jedis ...

  10. OpenERP中自定义模块卸载失败,Postgres数据库删不掉数据库,OpenERP登录不了一直在加载的问题解决方案。

    解决方案也就是删除掉不用的数据库,OE会提示当前有N个Session不让Drop数据库. 对于Postgres 9.1 版本,在pgAdmin中查询以下语句: SELECT pg_terminate_ ...