zoukankan      html  css  js  c++  java
  • Android基础类之Bundle

    Bundle类是一个key-value对,“A mapping from String values to various Parcelable types.”

    1. 声明Bundle对象实例,压入键值对,并传递对象实例:

      /**
         * Indicate that the connection was lost and notify the UI Activity.
         */
        private void connectionLost() {
            // Send a failure message back to the Activity
            Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST);
            Bundle bundle = new Bundle();
            bundle.putString(BluetoothChat.TOAST, "Device connection was lost");
            msg.setData(bundle);
            mHandler.sendMessage(msg);
    
            // Start the service over to restart listening mode
            BluetoothChatService.this.start();
        }

    2. 获取并解析Bundle对象实例:

      // The Handler that gets information back from the BluetoothChatService
        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:
                        setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
                        mConversationArrayAdapter.clear();
                        break;
                    case BluetoothChatService.STATE_CONNECTING:
                        setStatus(R.string.title_connecting);
                        break;
                    case BluetoothChatService.STATE_LISTEN:
                    case BluetoothChatService.STATE_NONE:
                        setStatus(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
                    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;
                }
            }
        };
  • 相关阅读:
    Struts2学习总结——文件上传与下载
    String.StartsWith与 EndsWith在大量使用时效率果然极低
    使用CXF实现基于Rest方式的WebService
    使用CXF实现基于Soap协议的WebService
    Hibernate单向“一对多”关联
    Hibernate单向“多对一”关联
    Hibernate单向“一对一”关联
    Spring使用经验之Listener综述
    Eclipse3.4以上使用dropins的插件安装方式
    数据库第一、二、三范式
  • 原文地址:https://www.cnblogs.com/jayhust/p/4174122.html
Copyright © 2011-2022 走看看