The bundle of Android basic classes

Source: Internet
Author: User

The bundle class is a key-value pair, "a mapping from String values to various parcelable types."

1. Declare the Bundle object instance, press in the key-value pair, and pass the object instance:

/*** Indicate the connection was lost and notify the UI Activity. */    Private voidConnectionlost () {//Send A failure message back to the ActivityMessage 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 modeBluetoothchatservice. This. Start (); }

2. Obtain and parse the bundle object instance:

//The Handler that gets information back from the Bluetoothchatservice    Private FinalHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { CaseMessage_state_change:if(D) log.i (TAG, "Message_state_change:" +msg.arg1); Switch(MSG.ARG1) { CaseBluetoothChatService.STATE_CONNECTED:setStatus (getString (r.string.title_connected_to, Mconnectedd                    Evicename));                    Mconversationarrayadapter.clear ();  Break;  CaseBluetoothChatService.STATE_CONNECTING:setStatus (r.string.title_connecting);  Break;  CaseBluetoothchatservice.state_listen: CaseBluetoothChatService.STATE_NONE:setStatus (r.string.title_not_connected);  Break; }                 Break;  CaseMessage_write:byte[] Writebuf = (byte[]) msg.obj; //construct a string from the bufferString Writemessage =NewString (WRITEBUF); Mconversationarrayadapter.add ("Me:" +writemessage);  Break;  CaseMessage_read:byte[] Readbuf = (byte[]) msg.obj; //construct a string from the valid bytes in the bufferString Readmessage =NewString (readbuf, 0, MSG.ARG1); Mconversationarrayadapter.add (Mconnecteddevicename+":  " +readmessage);  Break;  CaseMessage_device_name://Save the connected device ' s nameMconnecteddevicename =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; }        }    };

The bundle of Android basic classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.