• 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;
                }
            }
        };
  • 相关阅读:
    FFF NOJ 2073 裸KM算法
    FFF NOJ 2073 裸KM算法
    FFF NOJ 2073 裸KM算法
    【HDOJ】3242 List Operations
    【HDOJ】2319 Card Trick
    【HDOJ】1760 A New Tetris Game
    【HDOJ】1525 Euclid's Game
    【HDOJ】2217 Visit
    【HDOJ】2144 Evolution
    【HDOJ】1987 Decoding
  • 原文地址:https://www.cnblogs.com/jayhust/p/4174122.html
Copyright © 2020-2023  润新知