第二章——第二节 IPC机制的概述和使用
一、Serialiable与Paracle
①、作用 ②、使用
二、Binder与AIDL
①、各自的作用
三、如何使用IPC机制
举例
四、IPC机制的原理
①、流程图 ②、自己编译自动生成的Aidl代码
回答:
Serialiable的使用
步骤:1、创建类并继承Serializable接口 2、将对象序列化到文件中 3、从文件中获取序列化的文件
详细步骤:
1、创建Book类,并继承Serializable,编写serialVersionUID降低反序列化失败的概率
public class Book implements Serializable { private static final long serialVersionUID = 0X1111L; private String name; private String author; private String publish; public Book(String author, String name, String publish) { this.author = author; this.name = name; this.publish = publish; } @Override public String toString() { return "Book{" + "author='" + author + '\'' + ", name='" + name + '\'' + ", publish='" + publish + '\'' + '}'; } }
2、通过Android自带的文件储存器保存文件。
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Book book = new Book("asd","zxc","qweqw"); try { //利用自带的类,创建文件 FileOutputStream stream = openFileOutput("serialiable",MODE_APPEND); ObjectOutputStream dos = new ObjectOutputStream(stream); //将Book对象序列化 dos.writeObject(book); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //layout中对Button使用 android:onClick="launchSecondActivity" public void launchSecondActivity(View view){ Intent intent = new Intent(this,SecondActivity.class); startActivity(intent); } }
3、获取文件的流,并读取文件
public class SecondActivity extends Activity { private static final String TAG = "SecondActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); try { FileInputStream stream = openFileInput("serialiable"); ObjectInputStream ois = new ObjectInputStream(stream); Book book = (Book) ois.readObject(); Log.d(TAG,book+""); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (StreamCorruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
Parcelable的使用
步骤:1.创建Book类,并继承Parcelable 2、重写Parcelable的接口 3、创建Creator
详细步骤:
public class Book implements Parcelable { private String name; private String author; private String publish; public Book(String author, String name, String publish) { this.author = author; this.name = name; this.publish = publish; } public Book(Parcel parcel){ this.author = parcel.readString(); this.name = parcel.readString(); this.publish = parcel.readString(); } @Override public String toString() { return "Book{" + "author='" + author + '\'' + ", name='" + name + '\'' + ", publish='" + publish + '\'' + '}'; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { } }
②、重写writeToParcel()
@Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(author); dest.writeString(name); dest.writeString(publish); }
③、创建获取器Creator
public Book(Parcel parcel){ this.author = parcel.readString(); this.name = parcel.readString(); this.publish = parcel.readString(); } public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>(){ @Override public Book createFromParcel(Parcel source) { return new Book(source); } @Override public Book[] newArray(int size) { return new Book[0]; } };
完整代码
public class Book implements Parcelable { private String name; private String author; private String publish; public Book(String author, String name, String publish) { this.author = author; this.name = name; this.publish = publish; } @Override public String toString() { return "Book{" + "author='" + author + '\'' + ", name='" + name + '\'' + ", publish='" + publish + '\'' + '}'; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(author); dest.writeString(name); dest.writeString(publish); } public Book(Parcel parcel){ this.author = parcel.readString(); this.name = parcel.readString(); this.publish = parcel.readString(); } public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>(){ @Override public Book createFromParcel(Parcel source) { return new Book(source); } @Override public Book[] newArray(int size) { return new Book[0]; } }; }
IPC的简单使用:
任务:完成多进程间Book的添加和获取
步骤1、在java文件夹下创建aidl文件夹(需要进行IPC的文件都放在这个地方)
步骤2、在aidl文件夹下创建Book,并继承Parcelable
(1)、创建Book类
public class Book { private String author; private String name; private String publish; public Book(String author, String name, String publish) { this.author = author; this.name = name; this.publish = publish; } @Override public String toString() { return "Book{" + "author='" + author + '\'' + ", name='" + name + '\'' + ", publish='" + publish + '\'' + '}'; } }
(2)、继承Parcelable
public class Book implements Parcelable{ private String author; private String name; private String publish; public Book(String author, String name, String publish) { this.author = author; this.name = name; this.publish = publish; } @Override public String toString() { return "Book{" + "author='" + author + '\'' + ", name='" + name + '\'' + ", publish='" + publish + '\'' + '}'; } //不用修改 @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(author); dest.writeString(name); dest.writeString(publish); } public static Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>(){ @Override public Book createFromParcel(Parcel source) { return new Book(source); } @Override public Book[] newArray(int size) { return new Book[0]; } }; //读取的顺序跟保存的顺序需要一致 public Book(Parcel parcel){ author = parcel.readString(); name = parcel.readString(); publish = parcel.readString(); } }
步骤3、在aidl文件夹中声明Book(只有在aidl声明的类才能在aidl中调用),会生成src/main下的aidl文件夹
package aidl; // Declare any non-default types here with import statements parcelable Book;
步骤4、在src/main/aidl文件夹中创建IBookManager
// IBookManager.aidl package aidl; //需要自己添加import声明 import aidl.Book; // Declare any non-default types here with import statements interface IBookManager { //注意:需要填写in out inout 参数的类型 void addBook(in Book book); List getBook(); }
步骤5、创建Service,并实现Binder(IBookManager)
public class BookManagerService extends Service{ private static final String TAG = "BookManagerService"; private List<Book> mBookItems; @Override public void onCreate() { super.onCreate(); mBookItems = new ArrayList<>(); } @Nullable @Override public IBinder onBind(Intent intent) { return new IBookManager.Stub(){ @Override public void addBook(Book book) throws RemoteException { mBookItems.add(book); Log.d(TAG,book+" "); } @Override public List getBook() throws RemoteException { return mBookItems; } }; } }
步骤6、测试,Binder死亡时候重新创建的使用
public class MainActivity extends AppCompatActivity { private IBookManager manager; private ServiceConnection connection; //当Binder被以外终止的时候调用 private IBinder.DeathRecipient deathRecipient = new IBinder.DeathRecipient() { @Override public void binderDied() { if (manager != null){ manager.asBinder().unlinkToDeath(deathRecipient,0); manager = null; } //接下来重新binderService createService(); Intent intent = new Intent(MainActivity.this, BookManagerService.class); bindService(intent,connection, Service.BIND_AUTO_CREATE); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createService(); } private void createService() { connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { manager = IBookManager.Stub.asInterface(service); Book book = new Book("asd","zxc","qweqw"); try { manager.addBook(book); } catch (RemoteException e) { e.printStackTrace(); } try { //绑定死亡接口 service.linkToDeath(deathRecipient, 0); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { //以外终止时候调用 } }; } public void launchSecondActivity(View view){ Intent intent = new Intent(this, BookManagerService.class); bindService(intent,connection, Service.BIND_AUTO_CREATE); } }