Android中跨进程通信方式之使用文件共享

来源:互联网 发布:世界域名 编辑:程序博客网 时间:2024/04/29 15:35

一、使用Serializable序列化对象

import java.io.Serializable;public class User implements Serializable{    private static final long serialVersionUID  = 1L;    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

serialVersionUID是序列化对象时向文件写入的一个UID,其目地是为了反序列化恢复该对象时,防止增加或删除一些字段或者改动某个类型,导致类的版本不同,恢复错误。
序列化过程:

        User user = new User();        user.setUsername("zhangsan");        user.setPassword("123");        ObjectOutputStream os = null;        try {            os = new ObjectOutputStream(new FileOutputStream(getFilesDir()+"cache.txt"));            os.writeObject(user);        } catch (Exception e) {            e.printStackTrace();        }finally{            try {                os.close();            } catch (IOException e) {                e.printStackTrace();            }        }

反序列化恢复过程:

        ObjectInputStream is = null;        try {            is = new ObjectInputStream(new FileInputStream(getFilesDir()+"cache.txt"));            User newUser = (User) is.readObject();            tv.setText(newUser.getUsername());        } catch (Exception e) {            e.printStackTrace();        } finally{            try {                is.close();            } catch (IOException e) {                e.printStackTrace();            }        }

二、使用Parcelable序列化对象

import android.os.Parcel;import android.os.Parcelable;public class Student implements Parcelable{    private String username;    private String password;    public Student(){    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(username);        dest.writeString(password);    }    public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() {        @Override        public Student[] newArray(int size) {            return new Student[size];        }        @Override        public Student createFromParcel(Parcel source) {            return new Student(source);        }    };    private Student(Parcel source){        username = source.readString();        password = source.readString();    }}

writeToParcel方法将实现序列化,通过Parcel的write方法实现。CREATOR 实现反序列化,通过Parcel的read方法实现。

这样便可以通过Intent,bundle传递Parcelable对象了。

                Intent intent = new Intent();                intent.setClass(MainActivity.this, SecondActivity.class);                Bundle bundle = new Bundle();                Student student = new Student();                student.setUsername("lisi");                student.setPassword("456");                bundle.putParcelable("student", student);                intent.putExtras(bundle);                startActivity(intent);

Serializable需要大量的I/O操作,需要将序列化的对象存储在设备或者网络传输时适用。
Parcelable是安卓的序列化方式,使用比较麻烦,但效率高。

注意:当Parcelable对象中有对象时,这个对象必须是Parcelable或者Serializable类型的。
序列化方式如下:

//user为Serializableprivate User user;dest.writeSerializable(user);user = (User) source.readSerializable();//teacher为Parcelableprivate Teacher teacher;dest.writeParcelable(teacher, 0);//传递当前线程的上下文类加载器teacher = source.readParcelable(Thread.currentThread().getContextClassLoader());
0 0
原创粉丝点击