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