共享文件也是一种不错的IPC方式,两个进程通过读写同一个文件来交换数据,比如A进程把数据写入文件,B进程通过读取文件来获取数据。由于Android基于Linux,所以并发读写没有什么限制,甚至线程同时读写文件都可以,尽管会出现问题。利用这个思想,我们可以序列化一个对象到文件系统中,同时另一个进程中回复这个对象。
还是以上一个工程为例:
在MainActivity添加以下代码:
- @Override
- protected void onResume() {
- persistToFile();
- super.onStart();
- }
-
- private void persistToFile() {
- new Thread(new Runnable() {
-
- @Override
- public void run() {
- String path = Environment.getExternalStorageDirectory().getPath()
- + "/qian/";
- User user = new User(1, "hello world", false);
- File dir = new File(path);
- if (!dir.exists()) {
- dir.mkdirs();
- }
- File cachedFile = new File(path + "cache");
- ObjectOutputStream objectOutputStream = null;
- try {
- objectOutputStream = new ObjectOutputStream(
- new FileOutputStream(cachedFile));
- objectOutputStream.writeObject(user);
- Log.i(TAG, "persist user:" + user);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
-
-
- try {
- if(objectOutputStream != null)
- objectOutputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- }
- }
- }).start();
- }
在BundleActivty添加如下代码:
- @Override
- protected void onResume() {
- super.onResume();
-
- Log.d(TAG, "onResume");
- recoverFromFile();
- }
-
- private void recoverFromFile() {
- new Thread(new Runnable() {
-
- @Override
- public void run() {
- User user = null;
- String path = Environment.getExternalStorageDirectory().getPath()
- + "/qian/";
- File cachedFile = new File(path + "cache");
- if (cachedFile.exists()) {
- ObjectInputStream objectInputStream = null;
- try {
- objectInputStream = new ObjectInputStream(
- new FileInputStream(cachedFile));
- user = (User) objectInputStream.readObject();
- Log.i(TAG, "recover user:" + user);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- if(objectInputStream != null)
- objectInputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }).start();
- }
其实一个序列化与反序列化的过程,其中定义了一个可序列化的对象User,继承自Parcelable, Serializable接口,这样便可以序列化,User代码如下:
- package com.qian.ipc;
-
- import java.io.Serializable;
-
-
-
- import android.os.Parcel;
- import android.os.Parcelable;
-
- public class User implements Parcelable, Serializable {
- private static final long serialVersionUID = 519067123721295773L;
-
- public int userId;
- public String userName;
- public boolean isMale;
-
- public Book book;
-
- public User() {
- }
-
- public User(int userId, String userName, boolean isMale) {
- this.userId = userId;
- this.userName = userName;
- this.isMale = isMale;
- }
-
- public int describeContents() {
- return 0;
- }
-
- public void writeToParcel(Parcel out, int flags) {
- out.writeInt(userId);
- out.writeString(userName);
- out.writeInt(isMale ? 1 : 0);
- out.writeParcelable(book, 0);
- }
-
- public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
- public User createFromParcel(Parcel in) {
- return new User(in);
- }
-
- public User[] newArray(int size) {
- return new User[size];
- }
- };
-
- private User(Parcel in) {
- userId = in.readInt();
- userName = in.readString();
- isMale = in.readInt() == 1;
- book = in
- .readParcelable(Thread.currentThread().getContextClassLoader());
- }
-
- @Override
- public String toString() {
- return String.format(
- "User:{userId:%s, userName:%s, isMale:%s}, with child:{%s}",
- userId, userName, isMale, book);
- }
-
- }
最终打印的Log如下:只要红色框圈住的,其中红色框中第二栏Application打印的是进程名,可以看出MainActivity和BundleActivity是属于不同进程的。很显然,BundleActivity成功的从文件中回复了之前存储的User对象的内容,这里之所以说内容,是因为反序列化得到的对象只是在内容上和序列化之前的对象式样的,但他们本质上是两个对象。
![](https://img-blog.csdn.net/20160616222222269)
通过文件共享这种方式来共享数据对文件格式是没有具体要求的,比如可以是文本文件,也可以是xml文件,只要读写双方约定数据格式即可,也不一定要用序列化对象这种方式来实现文件共享,但是问题是要处理并发读写的问题。
另外SharePreferences是个特例,它是Android中提供的轻量级存储方案,通过键值对的方式存储数据,在底层实现上采用xml文件来存储键值对。但是SharePreferences也有一个问题,但是由于系统对SharePreferences的读写有一定的缓存策略,也就是说在内存中会有一份SharePreferences文件的缓存,因此在多进程模式下,系统对它的读写就变得不可靠,当面对高并发的读写访问SharePreferences有很大几率会丢失数据。