Bundle数据传输
重要方法
clear():清除此Bundle映射中的所有保存的數據。
clone():克隆當前Bundle
containsKey(String key):返回指定key的值
getString(String key):返回指定key的字符
hasFileDescriptors():指示是否包含任何捆綁打包文件描述符
isEmpty():如果這個捆綁映射為空,則返回true
putString(String key, String value):插入一個給定key的字符串值
readFromParcel(Parcel parcel):讀取這個parcel的內容
remove(String key):移除指定key的值
writeToParcel(Parcel parcel, int flags):寫入這個parcel的內容
Intent消息傳遞
?? 1、直接調用putExtra()方法將信息添加到Extra屬性中,然后通過調用getXXXExtra()方法進行獲取即可。這種方式比較簡單、直接,主要用于數據量比較少的情況下。
?? 例如:
Activity1中存數據:
Intent intent = new Intent(Activity1.this, Activity2.class);intent.putExtra("name","jack");startActivity(intent);?
Activity2中去數據:
Intent myintent=this.getIntent();String Name=myintent.getStringExtra("name");?
2、先將數據封裝到Bundle包中,Bundle可以看成是一個“鍵/值”映射的哈希表。當數據量比較多時,可以使用Bundle存放數據;然后通過putExtras()方法將Bundle對象添加到Extra屬性中,再通過使用getExtras()方法獲取存放的Bundle對象,最后讀取Bundle包中的數據。這種方式是簡介通過Bundle包對數據先進行封裝,再進行傳遞,實現起來比較繁瑣,因此,主要用于數據量較多的情況。
例如:
Activity1中:
Intent myintent=new Intent();myintent.setClass(Activity1.this,Activity2.class);Bundle mybundle=new Bundle();mybundle.putString("name","jace");mybundle.putInt("age",40);myintent.putExtras(mybundle);Activity1.this.startActivity(myintent);?
Activity2中:
Intent myintent=this.getIntent();bundle mybundle=myintent.getExtras();String Name=mybundle.getString("name");Int Age=mybundle.getInt("age");?Bundle在Handler中的數據傳輸
發送消息:
//發送一個消息到Handler發送一個BluetoothChat.MESSAGE_STATE_CHANGE消息到UI線程中//對應BluetoothChat的mHandlermHandler.obtainMessage(BluetoothChat.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();?或
//發送鏈接的設備名稱到UI Activity界面Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME);Bundle bundle = new Bundle();bundle.putString(BluetoothChat.DEVICE_NAME, device.getName());msg.setData(bundle);mHandler.sendMessage(msg);?或
Message msg = myHandler.obtainMessage();//將msg發送到目標對象,所謂的目標對象,就是生成該msg對象的handler對象Bundle b = new Bundle();b.putInt("age", 20);b.putString("name", "Jhon");msg.setData(b);msg.sendToTarget();?獲取消息
private final Handler mHandler = new Handler() {@Overridepublic 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:mTitle.setText(R.string.title_connected_to);//設置狀態為已經連接mTitle.append(mConnectedDeviceName);//添加設備名稱 標題為鏈接的設備名稱mConversationArrayAdapter.clear();//清理聊天記錄break;case BluetoothChatService.STATE_CONNECTING:mTitle.setText(R.string.title_connecting);break;case BluetoothChatService.STATE_LISTEN:case BluetoothChatService.STATE_NONE:mTitle.setText(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//保存鏈接的設備名稱,并顯示一個toast提示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;}}};?或
public void handleMessage(Message msg) {Bundle b = msg.getData();int age = b.getInt("age");String name = b.getString("name");System.out.println("age is " + age + ", name is" + name);System.out.println("Handler--->" + Thread.currentThread().getId());System.out.println("handlerMessage");}?
轉載于:https://www.cnblogs.com/qingblog/archive/2012/06/27/2565497.html
總結
以上是生活随笔為你收集整理的Bundle数据传输的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle Enterprise Li
- 下一篇: 重庆市公安局北碚分局所需容灾备份设备及软