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;
                }
            }
        };
  • 相关阅读:
    UITextField小结
    由iPhone项目生成iPad项目
    ios界面动画小结
    UIImagePickerController Class
    Xcode调试相关小结
    cocos2d简易引导
    python 多个装饰器组合应用,实现面向切面之AOP编程
    python 元类型编程, 单例模式SingleTon的一种实现方式
    python 实现简单的PerformanceCountCallHandler装饰器
    wxpython 之 GDI(二)
  • 原文地址:https://www.cnblogs.com/jayhust/p/4174122.html
Copyright © 2011-2022 走看看