博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bundle数据传输
阅读量:7234 次
发布时间:2019-06-29

本文共 4642 字,大约阅读时间需要 15 分钟。

重要方法

    clear():清除此Bundle映射中的所有保存的数据。
    clone():克隆当前Bundle
    containsKey(String key):返回指定key的值
    getString(String key):返回指定key的字符
    hasFileDescriptors():指示是否包含任何捆绑打包文件描述符
    isEmpty():如果这个捆绑映射为空,则返回true
    putString(String key, String value):插入一个给定key的字符串值
    readFromParcel(Parcel parcel):读取这个parcel的内容
    remove(String key):移除指定key的值
    writeToParcel(Parcel parcel, int flags):写入这个parcel的内容

Intent消息传递

   1、直接调用putExtra()方法将信息添加到Extra属性中,然后通过调用getXXXExtra()方法进行获取即可。这种方式比较简单、直接,主要用于数据量比较少的情况下。

   例如:

Activity1中存数据:

Intent intent = new Intent(Activity1.this, Activity2.class);intent.putExtra("name","jack");startActivity(intent);

 

Activity2中去数据:

Intent myintent=this.getIntent();String Name=myintent.getStringExtra("name");

 

2、先将数据封装到Bundle包中,Bundle可以看成是一个“键/值”映射的哈希表。当数据量比较多时,可以使用Bundle存放数据;然后通过putExtras()方法将Bundle对象添加到Extra属性中,再通过使用getExtras()方法获取存放的Bundle对象,最后读取Bundle包中的数据。这种方式是简介通过Bundle包对数据先进行封装,再进行传递,实现起来比较繁琐,因此,主要用于数据量较多的情况。

例如:

Activity1中:

Intent myintent=new Intent();myintent.setClass(Activity1.this,Activity2.class);Bundle mybundle=new Bundle();mybundle.putString("name","jace");mybundle.putInt("age",40);myintent.putExtras(mybundle);Activity1.this.startActivity(myintent);

 

Activity2中:

Intent myintent=this.getIntent();bundle mybundle=myintent.getExtras();String Name=mybundle.getString("name");Int Age=mybundle.getInt("age");

 Bundle在Handler中的数据传输

发送消息:

//发送一个消息到Handler发送一个BluetoothChat.MESSAGE_STATE_CHANGE消息到UI线程中        //对应BluetoothChat的mHandler        mHandler.obtainMessage(BluetoothChat.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();

 或

//发送链接的设备名称到UI Activity界面        Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME);        Bundle bundle = new Bundle();        bundle.putString(BluetoothChat.DEVICE_NAME, device.getName());        msg.setData(bundle);        mHandler.sendMessage(msg);

 或

Message msg = myHandler.obtainMessage();  //将msg发送到目标对象,所谓的目标对象,就是生成该msg对象的handler对象  Bundle b = new Bundle();  b.putInt("age", 20);  b.putString("name", "Jhon");  msg.setData(b);  msg.sendToTarget();

 获取消息

private final Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            switch (msg.what) {            case MESSAGE_STATE_CHANGE:                if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);                switch (msg.arg1) {                case BluetoothChatService.STATE_CONNECTED:                    mTitle.setText(R.string.title_connected_to);//设置状态为已经连接                    mTitle.append(mConnectedDeviceName);//添加设备名称 标题为链接的设备名称                    mConversationArrayAdapter.clear();//清理聊天记录                    break;                case BluetoothChatService.STATE_CONNECTING:                    mTitle.setText(R.string.title_connecting);                    break;                case BluetoothChatService.STATE_LISTEN:                case BluetoothChatService.STATE_NONE:                    mTitle.setText(R.string.title_not_connected);                    break;                }                break;            case MESSAGE_WRITE:                byte[] writeBuf = (byte[]) msg.obj;                // construct a string from the buffer               //将自己写入的消息也显示到会话列表中                String writeMessage = new String(writeBuf);                mConversationArrayAdapter.add("Me:  " + writeMessage);                break;            case MESSAGE_READ:                byte[] readBuf = (byte[]) msg.obj;                // construct a string from the valid bytes in the buffer              //取得内容并添加到聊天对话列表中                String readMessage = new String(readBuf, 0, msg.arg1);                mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);                break;            case MESSAGE_DEVICE_NAME:                // save the connected device's name            	//保存链接的设备名称,并显示一个toast提示                mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);                Toast.makeText(getApplicationContext(), "Connected to "                               + mConnectedDeviceName, Toast.LENGTH_SHORT).show();                break;            case MESSAGE_TOAST:            	//处理链接(发送)失败的消息                Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),                               Toast.LENGTH_SHORT).show();                break;            }        }    };

 或

public void handleMessage(Message msg) {   Bundle b = msg.getData();   int age = b.getInt("age");   String name = b.getString("name");   System.out.println("age is " + age + ", name is" + name);   System.out.println("Handler--->" + Thread.currentThread().getId());   System.out.println("handlerMessage");  }

 

转载地址:http://cplfm.baihongyu.com/

你可能感兴趣的文章
跨页面通信的各种姿势
查看>>
Java 开发者最容易犯的10个错误
查看>>
Web 探索之旅 | 第三部分第一课:服务器
查看>>
0110 - 给 iPhone 6 换了电池
查看>>
Android-Rxjava+Retrofit2.x 获取Http状态码、响应头(Headers)等数据
查看>>
swift版indexOfObject()
查看>>
第二十九章:基于SpringBoot平台使用Lombok来优雅的编码
查看>>
第三章:SpringBoot使用SpringDataJPA完成CRUD
查看>>
React + Redux + react router技术栈架构
查看>>
Android任务队列使用
查看>>
Swift语法对编译速度的影响
查看>>
如何在Python下搭建QT+SIP+PyQt5环境
查看>>
说说在 Linux 中如何查看系统信息
查看>>
iphone 常用的app info plist设置
查看>>
快速排序算法的实现
查看>>
傻瓜式入门Redux
查看>>
最新图解 如何提升phpstudy中的mysql版本
查看>>
华山论剑之iOS&tableView的双剑合璧
查看>>
4K超清,2500万人在线,猫晚直播技术全解读
查看>>
人人都能学会的python编程教程2:数据类型和变量
查看>>