标签:
一、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的简单使用
标签:
原文地址:http://www.cnblogs.com/rookiechen/p/5701584.html