码迷,mamicode.com
首页 > 其他好文 > 详细

第二章——第二节 IPC机制的概述和使用

时间:2016-07-24 22:17:47      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

一、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 + ‘\‘‘ +
                ‘}‘;
    }
}
Book

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);
    }
}
MainActivity

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();
        }
    }
}
SecondActivity

 

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) {
    }
}
Book

②、重写writeToParcel()

技术分享
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(author);
        dest.writeString(name);
        dest.writeString(publish);
    }
Book

③、创建获取器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];
        }
    };
Book

完整代码

技术分享
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];
        }
    };
}
Book

 

IPC的简单使用

 

第二章——第二节 IPC机制的概述和使用

标签:

原文地址:http://www.cnblogs.com/rookiechen/p/5701584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!