第五章 以資料為中心—資料存取(3),第五章存取

來源:互聯網
上載者:User

第五章 以資料為中心—資料存取(3),第五章存取
5.1.3自由操作隨心所欲—序列化和還原序列化

在Android開發中,除了經常會操作普通的文字檔和XML檔案以外,也會經常使用序列化和還原序列化的方式傳遞或者存取資料。

Android序列化對象主要有兩種方法,實現Serializable介面、或者實現Parcelable介面。實現Serializable介面是Java SE本身就支援的,而Parcelable是Android特有的功能,效率比實現Serializable介面高,而且還可以用在IPC中。實現Serializable介面非常簡單,聲明一下就可以了,而實現Parcelable介面稍微複雜一些,但效率更高,推薦用這種方法提高效能。

下面就介紹一下使用Parcelable介面,實現兩個Activity之間對象的傳遞,這裡就要用到bundle.putParcelable實現傳遞對象。

1)聲明實現介面Parcelable。

// import略

public class Person implements Parcelable{ 

 

    protected String name;

    protected String age;

 

    Person(String name,String age) { 

        this.name = name; 

        this.age = age; 

    } 

    Person(Parcel in) { 

        name = in.readString(); 

        age = in.readString();  

    } 

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getAge() {

        return age;

    }

    public void setAge(String age) {

        this.age = age;

    }

    @Override

    public int describeContents() {

        return 0;

    }

    /**

     * 實現Parcelable的方法writeToParcel,將你的對象序列化為一個Parcel對象

     */

    @Override

    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(name); 

        dest.writeString(age); 

    }

    /**

     * 執行個體化靜態內部對象CREATOR實現介面Parcelable.Creator

     */

    public static final Creator<Person> CREATOR = new Creator<Person>() { 

        public Person createFromParcel(Parcel in) { 

            return new Person(in); 

        } 

        public Person[] newArray(int size) { 

            return new Person[size]; 

        } 

    }; }

 

2)實現Parcel對象序列化為你的對象,並將Parcelable放入Bundle中。

// import略

public class ParcelableActivity1  extends Activity {

 

    private Button myButton; 

 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        myButton = new Button(this); 

        myButton.setOnClickListener(new OnClickListener() { 

            @Override 

            public void onClick(View arg0) { 

            // 建立Person對象

            Person benParcelable = new Person("testname","testage"); 

                Intent intent = new Intent(); 

                intent.setClass(getApplicationContext(),

                                ParcelableActivity2.class); 

                Bundle bundle = new Bundle(); 

                // 將序列化對象放入bundle

                bundle.putParcelable("person", benParcelable); 

                intent.putExtras(bundle); 

                // 啟動ParcelableActivity2的Activity

                startActivity(intent); 

            } 

        }); 

        setContentView(myButton); 

    } 

}

 

3)實現方法createFromParcel,將Parcel對象還原序列化為你的對象。

// import略

public class ParcelableActivity2 extends Activity {

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

        // 擷取還原序列化後的Person對象

        Person parcelable = getIntent().getParcelableExtra("person"); 

        // 列印輸出

        System.out.println(parcelable.getName()); 

        System.out.println(parcelable.getAge()); 

    } 

}

 --------------------------------------------程式員賺錢不易 一定要學會理財平安陸金所 隸屬於平安集團的p2p平台年投資報酬率7%-9% 是替代銀行理財的首選個人經驗 推薦投資安鑫或者有擔保的彩虹項目不要投資安e 那個幾乎無法轉讓 想提前提現非常困難網站連結 http://affiliate.lufax.com/action/36XBU首次投資1000元即可額外賺幾百元 不賺白不賺--------------------------------------------

4)顯示結果,5-2所示。

圖 5-2 序列化還原序列化的結果

 

經驗分享:

除了可以利用序列化和還原序列化在Activity之間傳遞Object類型的資料以外,我們也可以利用它來儲存和讀取資料。

和純文字檔案、XML檔案相比,序列化檔案可以是二進位的檔案,而不是純文字的,不能直接閱讀。所以,如果儲存的資料有一定的安全性要求,而安全性的層級又不是非常高,就可以考慮使用序列化的方式進行儲存,然後使用還原序列化的方式讀取。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.