android 進程間通信數據(一)------parcel的起源

關于parcel,我們先來講講它的“父輩”?Serialize。

Serialize 是java提供的一套序列化機制。但是為什么要序列化,怎么序列化,序列化是怎么做到的,我們將在本文探討下。

一:java 中的serialize

關于Serialize這個東東,think in java其實說的很詳細,大意如下:

1.Serialize的目的

當你創建對象時,你需要,它一直存在,但是當程序終止時,它就消失了。

如果程序不運行的情況下,可以保存某些信息,這將非常有用。

如何我程序在下次運行的時候,可以把上次運行的某些信息恢復回來.

2.Serialize的使用:

?使用一個嵌套的Serializable對象

package com.joyfulmath.androidstudy.bind.worm;import java.io.Serializable;public class Data implements Serializable {private int n;public Data(int n) {this.n = n;}@Overridepublic String toString() {return Integer.toString(n);}}
package com.joyfulmath.androidstudy.bind.worm;import java.io.Serializable;
import java.util.Random;import com.joyfulmath.androidstudy.TraceLog;public class Worm implements Serializable {static Random rand = new Random(47);Data[] d = {new Data(rand.nextInt(10)),new Data(rand.nextInt(10)),new Data(rand.nextInt(10))};private Worm next;private char c;public Worm(int i, char x){TraceLog.i("Worm construct:"+i);c = x;if(--i>0){next = new Worm(i,(char) (x+1));}}public Worm(){TraceLog.i("default Worm construct");}@Overridepublic String toString() {StringBuilder result = new StringBuilder(":");result.append(c);result.append("(");for(Data dat:d){result.append(dat+" ");}result.append(")");if(next!=null){result.append(next);}return result.toString();}}

驗證序列化的讀寫:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;import com.joyfulmath.androidstudy.TraceLog;public class WormSample {static final String path = "/mnt/sdcard/worm.out";public void doAction(){Worm w = new Worm(6, 'a');TraceLog.i("\n"+w.toString());try {ObjectOutputStream opt = new ObjectOutputStream(new FileOutputStream(path));opt.writeObject("Worm object\n");opt.writeObject(w);opt.close();ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));String s = (String) in.readObject();Worm w2 = (Worm) in.readObject();TraceLog.i(s+w);} catch (FileNotFoundException e) {// TODO Auto-generated catch block
            e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block
            e.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch block
            e.printStackTrace();}}
}

最后log:

08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:6 [at (Worm.java:21)]
08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:5 [at (Worm.java:21)]
08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:4 [at (Worm.java:21)]
08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:3 [at (Worm.java:21)]
08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:2 [at (Worm.java:21)]
08-15 09:18:20.384: I/Worm(28437): <init>: Worm construct:1 [at (Worm.java:21)]
08-15 09:18:20.384: I/WormSample(28437): doAction: 
08-15 09:18:20.384: I/WormSample(28437): :a(853):b(119):c(802):d(788):e(199):f(881) [at (WormSample.java:18)]
08-15 09:18:20.414: I/WormSample(28437): doAction: Worm object
08-15 09:18:20.414: I/WormSample(28437): :a(853):b(119):c(802):d(788):e(199):f(881) [at (WormSample.java:28)]

可以看到,數據被很好的還原了,包含內部的序列化對象!

?

二:parcel

Serializable是java定義的一套序列化機制,但是他是操作文件來執行的。或者說,它的性能無法滿足android上的要求,

這樣,parcel被google發明出來,用以取代Serializable。

1.Parcelable 的使用

package com.joyfulmath.androidstudy.bind.worm;import android.os.Parcel;
import android.os.Parcelable;public class DataP implements Parcelable {public int n;public DataP(int n) {this.n = n;}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeInt(n);}public static final Parcelable.Creator<DataP> CREATOR = new Parcelable.Creator<DataP>() {public DataP createFromParcel(Parcel in) {return new DataP(in);}public DataP[] newArray(int size) {return new DataP[size];}};private DataP(Parcel in) {n = in.readInt();}@Overridepublic String toString() {return Integer.toString(n);}}
package com.joyfulmath.androidstudy.bind.worm;import java.util.Random;import com.joyfulmath.androidstudy.TraceLog;import android.os.Parcel;
import android.os.Parcelable;public class WormP implements Parcelable {static Random rand = new Random(47);public DataP[] d = { new DataP(rand.nextInt(10)), new DataP(rand.nextInt(10)),new DataP(rand.nextInt(10)) };private WormP next;public byte c;public WormP(int i,byte x){TraceLog.i("Wormp construct:"+i);c = x;if(--i>0){next = new WormP(i,(byte) (x+1));}}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeByte(c);dest.writeParcelableArray(d, 0);if (next != null) {dest.writeParcelable(next, 0);}}public static final Parcelable.Creator<WormP> CREATOR = new Parcelable.Creator<WormP>() {public WormP createFromParcel(Parcel in) {return new WormP(in);}public WormP[] newArray(int size) {return new WormP[size];}};private WormP(Parcel in) {c = in.readByte();d = (DataP[]) in.readParcelableArray(DataP.class.getClassLoader());}@Overridepublic String toString() {StringBuilder result = new StringBuilder(":");result.append(c);result.append("(");for(DataP dat:d){result.append(dat+" ");}result.append(")");if(next!=null){result.append(next);}return result.toString();}
}

parcel一般使用在intent的內容的傳遞,所以本處做一個簡單的模擬:

    public void doActionP(){TraceLog.i();byte a = 'a';WormP w = new WormP(6, a);TraceLog.i(w.toString());Intent intent = new Intent();intent.putExtra("wormp", w);///...
        Intent newIntent = new Intent(intent);WormP w2 = newIntent.getParcelableExtra("wormp");TraceLog.i(w2.toString());TraceLog.i("end");}
08-15 10:14:11.924: I/WormSample(20183): doActionP:  [at (WormSample.java:47)]
08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:6 [at (WormP.java:21)]
08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:5 [at (WormP.java:21)]
08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:4 [at (WormP.java:21)]
08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:3 [at (WormP.java:21)]
08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:2 [at (WormP.java:21)]
08-15 10:14:11.934: I/WormP(20183): <init>: Wormp construct:1 [at (WormP.java:21)]
08-15 10:14:11.934: I/WormSample(20183): doActionP: :97(8 5 3 ):98(1 1 9 ):99(8 0 2 ):100(7 8 8 ):101(1 9 9 ):102(8 8 1 ) [at (WormSample.java:50)]
08-15 10:14:11.934: I/WormSample(20183): doActionP: :97(8 5 3 ):98(1 1 9 ):99(8 0 2 ):100(7 8 8 ):101(1 9 9 ):102(8 8 1 ) [at (WormSample.java:59)]
08-15 10:14:11.934: I/WormSample(20183): doActionP: end [at (WormSample.java:61)]

可以看到結果,數據完全正確。

以上就是parcel的使用方式,在下一篇,將探索parcel的實現方式。

?

參考:

http://blog.csdn.net/niu_gao/article/details/6451699

?

轉載于:https://www.cnblogs.com/deman/p/4742995.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258483.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258483.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258483.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

為什么torch.nn.Linear的表達形式為y=xA^T+b而不是常見的y=Ax+b?

今天看代碼&#xff0c;對比了常見的公式表達與代碼的表達&#xff0c;發覺torch.nn.Linear的數學表達與我想象的有點不同&#xff0c;于是思索了一番。 眾多周知&#xff0c;torch.nn.Linear作為全連接層&#xff0c;將下一層的每個結點與上一層的每一節點相連&#xff0c;用…

Leetcode47: Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. 推斷一個鏈表是不是回文的&#xff0c;一個比較簡單的辦法是把鏈表每一個結點的值存在vector里。然后首尾比較。時間復雜度O(n)。空間復雜度O(n)。 /*** Definition for singly-linked list.* struct ListNode {…

內存顆粒位寬和容量_SDRAM的邏輯Bank與芯片容量表示方法

1、邏輯Bank與芯片位寬講完SDRAM的外在形式&#xff0c;就該深入了解SDRAM的內部結構了。這里主要的概念就是邏輯Bank。簡單地說&#xff0c;SDRAM的內部是一個存儲陣列。因為如果是管道式存儲(就如排隊買票)&#xff0c;就很難做到隨機訪問了。陣列就如同表格一樣&#xff0c;…

[Unity菜鳥] Time

1. Time.deltaTime 增量時間 以秒計算&#xff0c;完成最后一幀的時間(秒)(只讀) 幀數所用的時間不是你能控制的。每一幀都不一樣&#xff0c;游戲一般都是每秒60幀&#xff0c;也就是updata方法調用60次&#xff08;假如你按60幀來算 而真實情況是不到60幀 那么物體就不會運動…

【轉】七個例子幫你更好地理解 CPU 緩存

我的大多數讀者都知道緩存是一種快速、小型、存儲最近已訪問的內存的地方。這個描述相當準確&#xff0c;但是深入處理器緩存如何工作的“枯燥”細節&#xff0c;會對嘗試理解程序性能有很大幫助。在這篇博文中&#xff0c;我將通過示例代碼來說明緩存是如何工作的&#xff0c;…

Pytorch——對應點相乘和矩陣相乘

1. 點乘&#xff0c;對應元素相乘&#xff0c;不求和 import torcha torch.Tensor([[1,2], [3,4], [5,6]]) b1 a.mul(a)// b2a*a b1 Out[79]: tensor([[ 1., 4.],[ 9., 16.],[25., 36.]]) b2 Out[80]: tensor([[ 1., 4.],[ 9., 16.],[25., 36.]]) 以上兩種方法都可以表…

mysql初始化錯誤【一】Can't find error-message file '/usr/local/mysql/errmsg.sys'

環境&#xff1a;CentOS 7.2MySQL 5.7.18從mysql官方網站下載rpm包到服務器本地&#xff0c;依次安裝下面的RPM包&#xff1a;mysql-community-common-5.7.18-1.el7.x86_64.rpmmysql-community-server-5.7.18-1.el7.x86_64.rpmmysql-community-client-5.7.18-1.el7.x86_64.rpmm…

雙極型adc與stm32_關于STM32 雙ADC同步規則轉換兩路數據的問題?

因系統要求需升級ADC的采樣方式(以前方式&#xff1a;掃描方式&#xff0c;TIMER2觸發ADC軟啟動&#xff0c;2通道規則序列&#xff0c;DMA傳完中斷)&#xff0c;為了進一步實現兩路信號的同步性能&#xff0c;采樣STM32 雙ADC同步規則轉換。(timer2觸發ADC軟啟動&#xff0c;2…

面試金典--11.5

題目描述&#xff1a;給定排序后的字符串數組&#xff0c;中間有一些空串&#xff0c;要求找到給定字符串的位置 思路&#xff1a; &#xff08;1&#xff09;遍歷&#xff0c;最慢的 &#xff08;2&#xff09;二分查找&#xff0c;當mid處為空串&#xff0c;就找到最近的非空…

win10 平臺VS2019最簡安裝實現C++/C開發

這兩天一直在安裝vs2015,總是卡在visual studio 2015 出現安裝包丟失或損壞的現象&#xff0c;盡管按照網上很多方法嘗試解決&#xff0c;但是一直不行。算了。還是使用最新版的VS 2019安裝&#xff0c;沒想到很順利。 下面總結一下在win10平臺上最簡安裝VS2019&#xff0c;實…

Hook的兩個小插曲

看完了前面三篇文章后&#xff0c;這里我們來一個小插曲~~~~ 第一個小插曲。是前面文章一個CM精靈的分析。我們這里使用hook代碼來搞定。 第二個小插曲&#xff0c;是如今一些游戲&#xff0c;都有了支付上限&#xff0c;比如每天僅僅能花20塊錢來購買。好了。以下我們分開敘述…

### C++總結-[類成員函數]

C類中的常見函數。 #author: gr #date: 2015-07-23 #email: forgeruigmail.com 一、constructor, copy constructor, copy assignment, destructor 1. copy constructor必須傳引用&#xff0c;傳值編譯器會報錯 2. operator 返回值為引用&#xff0c;為了…

微信小程序和vue雙向綁定哪里不一樣_個人理解Vue和React區別

本文轉載自掘金&#xff0c;作者&#xff1a;binbinsilk&#xff0c;監聽數據變化的實現原理不同Vue 通過 getter/setter 以及一些函數的劫持&#xff0c;能精確知道數據變化&#xff0c;不需要特別的優化就能達到很好的性能React 默認是通過比較引用的方式進行的&#xff0c;如…

JS 省,市,區

1 // 純JS省市區三級聯動2 // 2011-11-30 by http://www.cnblogs.com/zjfree3 var addressInit function (_cmbProvince, _cmbCity, _cmbArea, defaultProvince, defaultCity, defaultArea) {4 var cmbProvince document.getElementById(_cmbProvince);5 var cmbCity…

使用極鏈/AutoDL云服務器復盤caffe安裝

繼上一次倒騰caffe安裝以后&#xff0c;因為博士畢業等原因&#xff0c;舊的服務器已經不能再使用&#xff0c;最近因論文等原因&#xff0c;不得不繼續來安裝一下我的caffe。這次運氣比較好&#xff0c;經歷了一晚上和一早上的痛苦之后&#xff0c;最終安裝成功了&#xff0c;…

ibatis中使用List作為傳入參數的使用方法及 CDATA使用

ibatis中list做回參很簡單&#xff0c;resultClass設為list中元素類型&#xff0c;dao層調用: (List)getSqlMapClientTemplate().queryForList("sqlName", paraName); 并經類型轉換即可&#xff0c;做入參還需要稍微調整下&#xff0c;本文主要講list做入參碰到的幾…

Samba服務

####################samba####################1.samba作用提供cifs協議實現共享文件2.安裝yum install samba samba-common samba-client -ysystemctl start smb nmbsystemctl enable smb nmb3.添加smb用戶smb用戶必須是本機用戶[rootlocalhost ~]# smbpasswd -a student New…

wpf 窗口的返回值_WPF Tips: Window.ShowDialog() 返回 true

Window.ShowDialog() 返回值為bool?。希望在窗口點擊OK時返回True。解決方法&#xff1a;ShowDialog()的注釋為&#xff1a;// Returns:// A System.Nullable value of type System.Boolean that specifies whether// the activity was accepted (true) or canceled (false). …

CodeForces 543D 樹形DP Road Improvement

題意&#xff1a; 有一顆樹&#xff0c;每條邊是好邊或者是壞邊&#xff0c;對于一個節點為x&#xff0c;如果任意一個點到x的路徑上的壞邊不超過1條&#xff0c;那么這樣的方案是合法的&#xff0c;求所有合法的方案數。 對于n個所有可能的x&#xff0c;輸出n個答案。 分析&am…

理解Javascritp中的引用

Author: bugall Wechat: bugallF Email: 769088641qq.com Github: https://github.com/bugall一&#xff1a; 函數中的引用傳遞 我們看下下面的代碼的正確輸出是什么 function changeStuff(a, b, c) {a a * 10;b.item "changed";c {item: "changed"}; …