网友通过本文主要向大家介绍了android ipc,android ipc机制,android ipc是什么,android ipc通信,ipc文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android之使用文件进行IPC,android文件ipc
一、文件进行IPC介绍
共享文件也是一种不错的进程间通信方式,两个进程通过读/写同一个文件来交换数据。在Windows上,一个文件如果被加了排斥锁将会导致其他线程无法对其进行访问,包括读写,而由于Android系统基于Linux,使其并发读/写文件可以没有限制地进行,甚至两个线程同时对同一个文件进行读写操作是允许的,尽管这可能出现问题。通过文件交换数据很好使用,除了可以交换一些文本信息外,还可以序列化一个对象到文件系统中的同时从另一个进程中恢复这个对象。
二、使用方法
1.数据类实现Parcelable或Serializable接口
public class User implements Parcelable, Serializable { public User() { } public User(int userId, String userName, boolean isMale) { ... } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { ... } public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() { @Override public User createFromParcel(Parcel source) { return ...; } @Override public User[] newArray(int size) { return ...; } }; private User(Parcel in) { ... } @Override public String toString() { return ...; } }
2.序列化一个对象到sd卡上的一个文件里
private void persistToFile() { new Thread(new Runnable() { @Override public void run() { User user = new User(1, "hello world", false); File dir = new File(MyConstants.CHAPTER_2_PATH); if (!dir.exists()) { dir.mkdirs(); } File cachedFile = new File(MyConstants.CACHE_FILE_PATH); ObjectOutputStream objectOutputStream = null; try { objectOutputStream = new ObjectOutputStream(new FileOutputStream(cachedFile)); objectOutputStream.writeObject(user); Log.d(TAG, "persist user:" + user); mTextView.setText("persist user:" + user); } catch (IOException e) { e.printStackTrace(); } finally { MyUtils.close(objectOutputStream); } } }).start(); }
3.在另外的进程中反序列化
private void recoverFromFile(){ new Thread(new Runnable() { @Override public void run() { User user = null; File cachedFile = new File(MyConstants.CACHE_FILE_PATH); if(cachedFile.exists()){ ObjectInputStream objectInputStream = null; try{ objectInputStream = new ObjectInputStream(new FileInputStream(cachedFile)); user = (User)objectInputStream.readObject(); Log.d(TAG,"recover user:"+user); mTextView.setText("recover user:"+user); }catch (IOException e) { e.printStackTrace(); }catch (ClassNotFoundException e){ e.printStackTrace(); }finally { MyUtils.close(objectInputStream); } } } }).start(); }
4.在AndroidManifest.xml中开启多进程
<activity
...
android:process=":remote" />
三、小案例
1.修改activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.zhangmiao.ipcdemo.MainActivity" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="File"> </TextView> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="open activity B" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A"> </TextView> </LinearLayout>
2.添加activity_second.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="at activity B" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content"