Two serialization methods for Android Process Communication

Source: Internet
Author: User

Two serialization methods for Android Process Communication

I resigned in late February and went to Hainan for a holiday. Now, I have a taste of Sanya, which is also capricious and

However, the consequence of such willfulness is .. No, I have to say something ..

It's so difficult to find a job without graduation! I didn't have any chance to invest in 57 interviews. I 'd like to give me an interview !! This baby is unhappy !!

I don't know if this is the case for other children's shoes that haven't graduated. Is it really a cold winter .. In fact, I am not capable enough .. But don't be discouraged. As long as you work hard enough, you will have a job sooner or later (the logo suggests that I go to the plate first ???? Mom ).

The complaint was over. In order to improve myself better, I quickly opened the chairman's "Exploration of Development art" and began to learn about today's content. Before talking about Messanger, it is necessary to talk about serialization in Android.

Two serialization Methods

There are two serialization methods in Android: Serializable (forgive me for not spelling it until now ..) And Parcelable. Serializable is provided by java, and Parcelable is unique to Android.

The differences between the two types of serialization:
Parcelable is efficient, but it is difficult to use. It is mainly used for memory serialization.
Serializable is easy to use, but requires a lot of I/O operations. It is costly and suitable for storage devices or network transmission.

Use of Serializable

Serializable is easy to use. You only need to implement the Serializable interface.
You can use ObjectOutputStream and ObjectInputStream to perform serialization. For example:

public class Goods implements Serializable{    private int mId;    public Goods(int id){        mId = id;    }    public int getmId() {        return mId;    }}
Goods goods = new Goods (3); // serialize try {ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("/sdcard/test.txt"); oos. writeObject (goods); oos. close ();} catch (IOException e) {e. printStackTrace ();} // deserialization try {ObjectInputStream ois = new ObjectInputStream (new FileInputStream ("/sdcard/test.txt"); Goods goodsIn = (Goods) ois. readObject (); ois. close (); Log. e ("wing", goodsIn. getmId () + "");} catch (IOException e) {e. printStackTrace ();} catch (ClassNotFoundException e) {e. printStackTrace ();}

At this time, you can see that the output id of the console is 3.

It should be pointed out that in order to ensure the serialization correctness, A serialized UID should be added to the Goods class. That is, serialVersionUID. Here you can customize it, mainly the version check of the class. During the deserialization process, if the id value is inconsistent, deserialization fails.

private static final long serialVersionUID = 615555755L
Use of Parcelable

The use of parcelable is a little complicated. However, it is also very easy to have powerful. Take Goods as an example to add another name attribute.
Implement the Parcelable interface and generate the following code as prompted:

Public class Goods implements Parcelable {private int mId; private String mName; public Goods (int id, String name) {mId = id; mName = name;} protected Goods (Parcel in) {mId = in. readInt (); mName = in. readString ();} // automatically generated code public static final Creator
  
   
CREATOR = new Creator
   
    
() {@ Override public Goods createFromParcel (Parcel in) {return new Goods (in) ;}@ Override public Goods [] newArray (int size) {return new Goods [size] ;}}; public int getmId () {return mId ;}@ Override public int describeContents () {return 0 ;} @ Override public void writeToParcel (Parcel dest, int flags) {dest. writeInt (mId); dest. writeString (mName );}}
   
  

In this way, the serialization of a Parcel is realized. Of course, this is all generated by AS for you. Let's analyze the methods in it.
See the added constructor. The parameter is parcel, which gives the attribute Parcel. read (). For example, if your id is of the int type, it is id = in. readInt (); this part should be well understood.
The corresponding wirteToParcel interface writes these attributes.

When you use intent to transmit data, you can pass classes that implement serialization. Of course, the list and map classes can also be passed. Internal elements must also be serializable. It is worth noting that intent transmission has a fatal defect, that is, the size limit.

 

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.