zoukankan      html  css  js  c++  java
  • 浅谈Android中Serializable和Parcelable使用区别

    Android中序列化有两种方式:Serializable以及Parcelable。其中Serializable是Java自带的,而Parcelable是安卓专有的。

    一、Serializable序列化

    serializable使用比较简单,只需要对某个类实现Serializable 接口即可。

    Serializable 接口是一种标识接口,某个类实现Serializable 接口,Java便会对这个对象进行序列化操作。

    我们编写Person类:

    public class Person implements Serializable {
    
        private static final long serialVersionUID = -3139325922167935911L;
        //
        private int age;
        private String name;
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }

    很简单吧,无需过多解释。接下来我们就将这个类从一个Acticity传递到另一个Activity。

    MainActivity:

    public class MainActivity extends Activity {
    
        //
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //
            Person p = new Person();
            p.setAge(18);
            p.setName("wanglei");
            //
            Intent i = new Intent(this, SecondActivity.class);
            i.putExtra("person", p);
            startActivity(i);
            
        }
    }

     SecondActivity:

    public class SecondActivity extends Activity {
    
        //
    
        private static final String TAG = "WL";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Intent intent = getIntent();
            Person p=(Person) intent.getSerializableExtra("person");
            
            Log.i(TAG, "age = "+p.getAge());
            Log.i(TAG, "name = "+p.getName());
        }
    }

    以上代码及其简单了就不解释了,运行程序会看到如下打印:

    以上就是Serializable方式序列化对象的举例,真的很简单,没有什么多余要解释的。

    二、Parcelable序列化

    arcel 在英文中有两个意思,其一是名词,为包裹,小包的意思; 其二为动词,意为打包,扎包。邮寄快递中的包裹也用的是这个词。Android采用这个词来表示封装消息数据。这个是通过IBinder通信的消息的载 体。需要明确的是Parcel用来存放数据的是内存(RAM),而不是永久性介质(Nand等)。

    Parcelable,定义了将数据写入Parcel,和从Parcel中读出的接口。一个实体(用类来表示),如果需要封装到消息中去,就必须实现这一接口,实现了这一接口,该实体就成为“可打包的”了。

    关与Parcelable方式实现序列化会比Serializable 方法麻烦一些,大体步骤如下:

    1. 实现Parcelable接口
    2. 覆写describeContents方法,默认返回0。
    3. 覆写writeToParcel(Parcel dest, int flags)方法,指定写入Parcel类的数据。
    4. 创建Parcelable.Creator静态对象,覆写方法createFromParcel(Parcel in)与newArray(int size)。

    接口的定义如下:

    public interface Parcelable {
        //内容描述接口,基本不用管
        public int describeContents();
        //写入接口函数,打包
        public void writeToParcel(Parcel dest, int flags);
         //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入。
        //为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例。
        public interface Creator<T> {
               public T createFromParcel(Parcel source);
               public T[] newArray(int size);
        }
    }

    下面定义了一个简单类People, 他需要把自身的数据,打入包中。 同时在消息的接收方需要通过People实现的Parcelable接口,将People重新构造出来。

    People.java
    public class People implements Parcelable {
        private String name = null;
        private int age = 0;
        private int sex = 0;// 0代表男、1代表女
    
        public People() {
            super();
        }
    
        public People(String name, int age, int sex) {
            super();
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeInt(age);
            dest.writeInt(sex);
        }
    
        public static final Parcelable.Creator<People> CREATOR = new Creator<People>() {
    
            @Override
            public People[] newArray(int size) {
                return new People[size];
            }
    
            @Override
            public People createFromParcel(Parcel source) {
                return new People(source.readString(), source.readInt(),
                        source.readInt());
            }
        };
    }

    传递People

    //发送 MainActivity.java
    People people = new People();
    people.setName("张三");
    people.setSex(Integer.parseInt(0));
    people.setAge(Integer.parseInt(25));
    Intent intent = new Intent(MainActivity.this, ShowActivity.class);
    Bundle b = new Bundle();
    b.putParcelable("people", people);
    intent.putExtras(b);
    MainActivity.this.startActivity(intent);
    
    //接受 ShowActivity.java
    if(getIntent().getExtras().containsKey("people")){
      People people = getIntent().getExtras().getParcelable("people");
    }

    参考:

    https://www.cnblogs.com/xingfuzzhd/p/3454227.html

    https://www.cnblogs.com/leipDao/p/8022063.html

    我的GitHub:https://github.com/lelelongwang
  • 相关阅读:
    网易编程题——小易喜欢的单词
    Effective C++ 条款12:复制对象时勿忘其每一个成分
    Effective C++ 条款11:在operator=中处理"自我赋值"
    STM32-通用定时器基本定时功能
    GPIO_Mode
    STM32的ADC编程方法
    STM32的ADC采样与多通道ADC采样
    网络子系统
    linux网络子系统内核分析
    Linux 中高效编写 Bash 脚本的 10 个技巧
  • 原文地址:https://www.cnblogs.com/longjunhao/p/10266351.html
Copyright © 2011-2022 走看看