Bundle数据传输

重要方法
    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");
  }
原文地址:https://www.cnblogs.com/qingblog/p/2565497.html