`
1140566087
  • 浏览: 547674 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
博客专栏
2c4ae07c-10c2-3bb0-a106-d91fe0a10f37
c/c++ 入门笔记
浏览量:18076
3161ba8d-c410-3ef9-871c-3e48524c5263
Android 学习笔记
浏览量:309479
Group-logo
J2ME 基础学习课程集
浏览量:17993
A98a97d4-eb03-3faf-af96-c7c28f709feb
Spring 学习过程记录...
浏览量:17195
社区版块
存档分类
最新评论

Android 之 Parcelable 序列化

阅读更多
/*				序列化
 * 
 * 作用:序列化是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),
 * 并且可以把保存的对象状态再读出来。即:序列化是一种能比较好的保存对象状态
 * 的机制;
 * 
 * 优点:
 * 	1、永久性的保存对象,保存对象的字节序列到本地文件中;
 * 	2、通过序列化对象在网络中传递对象;
 * 	3、通过序列化在进程中传递对象;
 * 
 * 序列化方法:
 * 	1、实现Serializable 接口 ---- JAVA SE 本身支持的;
 * 	2、实现Parcelable 接口 ---- Android 特有的功能,效率比前者的高
 * 	
 * 	选取序列化方法的原因:
 * 		1、在使用内存的时候,Parcelable 类比 Serializable 性能高;
 * 		2、Serializable 在序列化的时候会产生大量的临时变量,从而引起频繁的GC;
 * 		3、Parcelable 不能使用在要将数据储存在磁盘上的情况,因为Parcelable 不
 * 		能很好的保证数据的持续性,在外界有变化的情况下;
 * 
 * 	使用方法:
 * 	1、Serializable 的实现,只需要实现Serializable 这个接口就行,系统会自动的序列化;
 * 	2、Parcelable 序列化实现:
 * 		a、声明实现接口Parcelable
 * 		b、实现Parcelable 的方法 writeToParcel ,将你的对象序列化为一个Parcel对象;
 * 		c、实例化静态内部对象CREATOR,实现接口 Parcelable.Creator
 * 			public static final Parcelable.Creator<T> CREATOR ,注意:public static
 * 			final 一个不能少,内部对象CREATOR 的名称也不能改变,必须全部大写;
 * 		d、完成了CREATOR 的代码,实现方法createFromParcel ,将 Parcel 对象反序列化为你的对象;
 * 
 * 		提示:通过writeToParcel 将你的对象映射成Parcel 对象,再通过createFromParcel将
 * 			parcel映射成你的对象,也可以将Parcel看成是一个流,通过writeToParcel 把对象写到
 * 			流里面,在通过createFromParcel 把对象从流里面读取对象,只不过这个过程需要自己来实现,
 * 			因此写的顺序和读的顺序必须一致;
 * 
 * 	3、支持的数据类型:byte double float int long String 六种; 以及他们的数组;
 * 
 *
 */

使用ParcelableData 序列化数据并进行传递:
package com.sun.parcelable;

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

public class ParcelableData implements Parcelable {

	private String name;
	private String[] names;
	private int age;
	private float score;
	private double money;
	private byte file; // -128 -127
	private long longname;
	

	//构造方法
	public ParcelableData(String name, String[] names, int age, float score,
			double money, byte file, long longname) {
		super();
		this.name = name;
		this.names = names;
		this.age = age;
		this.score = score;
		this.money = money;
		this.file = file;
		this.longname = longname;
	}


	public String[] getNames() {
		return names;
	}


	public void setNames(String[] names) {
		this.names = names;
	}


	public ParcelableData(){}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public float getScore() {
		return score;
	}

	public void setScore(float score) {
		this.score = score;
	}

	public double getMoney() {
		return money;
	}

	public void setMoney(double money) {
		this.money = money;
	}

	public byte getFile() {
		return file;
	}

	public void setFile(byte file) {
		this.file = file;
	}

	public long getLongname() {
		return longname;
	}

	public void setLongname(long longname) {
		this.longname = longname;
	}

	// 内容描述接口 , 默认返回 0 就可以了
	public int describeContents() {

		return 0;
	}

	// 读取接口,将 对象 序列化成 Parcel
	public void writeToParcel(Parcel dest, int flags) {
		Log.i("msg","writeToParcel() 进来了,准备开始对象序列化....");
		dest.writeString(name);
		dest.writeInt(age);
		dest.writeByte(file);
		dest.writeLong(longname);
		dest.writeDouble(money);
		dest.writeFloat(score);
		dest.writeStringArray(names);
		Log.i("msg","writeToParcel() 序列化完成,即将退出...");
	}
	
	/**
	 *  实例化静态内部对象CREATOR ,该对实现接口Parcelable.Creator
	 *  注意:public static final 一个都不能少,内部对象名称CREATOR 不能变,必须为大写
	 */
	public static final Parcelable.Creator<ParcelableData> CREATOR = new Creator<ParcelableData>() {

		// 将 Parcel 对象 反序列化为对象
		public ParcelableData createFromParcel(Parcel source) {
			Log.i("msg","createFromParcel() 进来了,准备开始反序列化....");
			ParcelableData pd = new ParcelableData();
			pd.setName(source.readString());
			pd.setAge(source.readInt());
			pd.setFile(source.readByte());
			pd.setLongname(source.readLong());
			pd.setMoney(source.readDouble());
			pd.setScore(source.readFloat());
			
			// 反序列 数组
			pd.setNames(source.createStringArray());
			Log.i("msg","createFromParcel() 反序列化完成,即将退出,返回对象....");
			return pd;
		}

		public ParcelableData[] newArray(int size) {

			return null;
		}

	};

}


发送序列化的数据:
package com.sun.parcelable;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 携带数据跳转 并显示
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, SecondActivity.class);
        ParcelableData pd = new ParcelableData();
        pd.setAge(12);
        pd.setFile(Byte.parseByte("123")); // -128 --- 127
        pd.setLongname(54455);
        pd.setMoney(11.001);
        pd.setName("卫斯理");
        pd.setNames(new String[]{"numbe1","number2"});
        pd.setScore(23.2f);
        Bundle bundle = new Bundle();
        Log.i("msg", "Bundle对象初始化完成,马上进入putParcelable 操作....");
        bundle.putParcelable("pd",pd);
        Log.i("msg","putParcelable() 存入数据完成....Intent 准备带入数据...");
        intent.putExtras(bundle);
        Log.i("msg","intent.putExtras(bundle) 完成...马上开始跳转...");
        startActivity(intent);
        Log.i("msg","跳转中....");
        
    }   
}


接收数据:

package com.sun.parcelable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

public class SecondActivity extends Activity{

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.secondactivity_layout);
		
		EditText show = (EditText) findViewById(R.id.show);
		Log.i("msg","开始获得传递过来的相关数据 , Intent bundle ....");
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		Log.i("msg","Intent对象获取完毕,下一步获取ParcelableData对象....");
		ParcelableData pd = bundle.getParcelable("pd");
		Log.i("msg","ParcelableData 对象获取完毕,下一步显示");
		show.setText(pd.getAge()+"\n"+""+pd.getFile()+"\n"+pd.getLongname()+"\n"+pd.getMoney()+"\n"+pd.getName()+"\n"+pd.getScore()+"\n"+pd.getNames().length);
		
	}

	
	
}
分享到:
评论
1 楼 泥沙爬虫 2014-02-17  
我又学到了,谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics