码迷,mamicode.com
首页 > 移动开发 > 详细

android基础---->Parcelable的使用

时间:2016-03-23 06:04:41      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

 android中Parcelable序列化的使用。

 

目录导航: 

  1.  Serializable在android中的使用
  2.  Parcelable在android中的使用
  3.  Serializable与Parcelable的比较
  4.  友情链接

 

项目结构如下:

 技术分享

Serializable在android中的使用

一、 建立一个实现了Serializable接口的Man类:

package com.example.linux.parcelabletest;

import java.io.Serializable;

/**
 * Created by Linux on 2016/3/22.
 */
public class Man implements Serializable {
    private String username;
    private String password;

    public Man(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

 

二、 MainActivity中serializable在android中的序列化

// serializable的使用
public void serializable(View view) {
    Intent intent = new Intent();
    intent.setClass(MainActivity.this, AnotherActivity.class);
    Man man = new Man("google", "android");
    Bundle bundle = new Bundle();
    bundle.putSerializable("man", man);
    intent.putExtras(bundle);
    startActivity(intent);
}

 

三、  AnotherActivity中serializable在android中的反序列化:

Intent intent = getIntent();
Bundle extras = intent.getExtras();
Man man = (Man) extras.getSerializable("man");
Log.i(TAG, "username: " + man.getUsername() + ", password: " + man.getPassword());

 

四、 打印结果如下:  

03-23 00:46:27.870 30378-30378/com.example.linux.parcelabletest I/Parcelable: username: google, password: android

 

Parcelable在android中的使用

一、 建立一个实现了Parcelable接口的Person类:

package com.example.linux.parcelabletest;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

/**
 * Created by Linux on 2016/3/20.
 */
public class Person implements Parcelable {
    private final static String TAG = "Parcelable";
    private String username;
    private String password;

    public Person(String username, String password) {
        Log.i(TAG, "person constructor. username and password"); // 1
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        Log.i(TAG, "get usrename");
        return username;
    }

    public String getPassword() {
        Log.i(TAG, "get password");
        return password;
    }

    @Override
    public int describeContents() {
        Log.i(TAG, "desceibe contents");
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        Log.i(TAG, "write to parcel: " + flags); // 2
        dest.writeString(username);
        dest.writeString(password);
    }

    public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel source) {
            Log.i(TAG, "creator: create from parcel"); // 3
            return new Person(source);
        }
@Override
public Person[] newArray(int size) { Log.i(TAG, "creator: new array"); return new Person[size]; } }; public Person(Parcel parcel) { Log.i(TAG, "person constructor. parcel"); // 4 username = parcel.readString(); password = parcel.readString(); } }

 

 二、 MainActivity中Parcelable在android中的序列化

// parcelable序列化的使用
    public void parcelable(View view) {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, AnotherActivity.class);
        Person person = new Person("huhx", "linux");
        Bundle bundle = new Bundle();
        bundle.putParcelable("person", person);
        intent.putExtras(bundle);
        startActivity(intent);
    }

 

三、  AnotherActivity中Parcelable在android中的反序列化:

Intent intent = getIntent();
Bundle extras = intent.getExtras();
Person person = extras.getParcelable("person");
Log.i(TAG, "username: " + person.getUsername() + ", password: " + person.getPassword());

 

四、 打印结果如下:

03-23 00:45:49.510 30378-30378/com.example.linux.parcelabletest I/Parcelable: person constructor. username and password
03-23 00:45:49.510 30378-30378/com.example.linux.parcelabletest I/Parcelable: write to parcel: 0
03-23 00:45:49.660 30378-30378/com.example.linux.parcelabletest I/Parcelable: creator: create from parcel
03-23 00:45:49.660 30378-30378/com.example.linux.parcelabletest I/Parcelable: person constructor. parcel
03-23 00:45:49.660 30378-30378/com.example.linux.parcelabletest I/Parcelable: get usrename
03-23 00:45:49.660 30378-30378/com.example.linux.parcelabletest I/Parcelable: get password
03-23 00:45:49.660 30378-30378/com.example.linux.parcelabletest I/Parcelable: username: huhx, password: linux

 

Serializable与Parcelable的比较

 

友情链接

  测试程序源代码               访问密码 7925

 

android基础---->Parcelable的使用

标签:

原文地址:http://www.cnblogs.com/huhx/p/androidParcelable.html

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