Android 用Intent 传递对象之 Parcel

Android 为对象序列化开发了 Parcelable 接口,这个接口要比之前说的 Serializable 更为高效。使用会稍微复杂些。

 

其主要思想就是把 对象需要保存的数据 放在 parcel 对象中,然后再从 parcel 中恢复这个对象。可见实现 Parcelable这个接口的类,至少会实现两个方法一个用来 将数据保存进 parcel 对象,一个 方法用来从 parcel 中恢复对象。

 

示例结构:

 

 先写一个实现了 Parcelable 接口的类 Person,可见 writeToParcel 用来向Parcel 对象写入要保存的对象,

CREATOR 用来恢复对象。存入的数据要和读取的数据顺序对应。

package com.serializabletest;

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

class Person implements Parcelable {

	private static final String TAG = "Person";
	int ID;
	String Name;
	int age;

	public Person(int ID, String Name, int age) {
		this.ID = ID;
		this.Name = Name;
		this.age = age;
	}

	public void printPerson() {
		Log.d(TAG, "ID:" + ID);
		Log.d(TAG, "Name:" + Name);
		Log.d(TAG, "age:" + age);
	}

	// -------------------------------------------------
	public Person(Parcel in) {
		this.ID = in.readInt();
		this.Name = in.readString();
		this.age = in.readInt();
	}

	// 从 Parcel 对象 中恢复 Person 对象
	<span style="color:#ff0000;"><strong>public static final Parcelable.Creator<Person> CREATOR</strong> </span>= new Parcelable.Creator<Person>() {

		@Override
		public Person createFromParcel(Parcel arg0) {
			// TODO Auto-generated method stub
			return new Person(arg0);
		}

		@Override
		public Person[] newArray(int size) {
			// TODO Auto-generated method stub
			return new Person[size];
		}
	};

	/**
	 * Describe the kinds of special objects contained in this Parcelable's
	 * marshalled(排列) representation(表现).
	 * 
	 * 用来描述序列化对象中特殊对象的分布状况。
	 * 
	 * Returns a bitmask indicating the set of special object types marshalled
	 * by the Parcelable.
	 * 
	 * @see android.os.Parcelable#describeContents()
	 */

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 用来将对象转换成 parcel 对象
	 * 
	 * 决定了把什么数据写入 parcel
	 * 
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	// 把 Person 对象转化为 Parcel 对象存储起来
	@Override
	public void <strong><span style="color:#ff0000;">writeToParcel</span></strong>(Parcel arg0, int arg1) {
		// TODO Auto-generated method stub

		arg0.writeInt(ID);
		arg0.writeString(Name);
		arg0.writeInt(age);
	}
}


MainActivity.java

package com.serializabletest;

import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {

	public static final String TAG = "MainActivity";
	/** 序列化测试
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	private final String objName = "/mnt/sdcard/object.txt";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("Intent 传递对象");
		setContentView(R.layout.activity_main);

	}

	public void onBtnClick(View view) throws IOException {
		Person person = new Person(1, "张三", 26);

		Bundle bundle = new Bundle();
		bundle.putParcelable("person", person);
		Intent intent = new Intent(MainActivity.this, SecondActivity.class);
		intent.putExtras(bundle);
		startActivity(intent);
	}
}


 

SecondActivity.java

package com.serializabletest;

import java.io.IOException;
import java.io.StreamCorruptedException;

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.Intent;

public class SecondActivity extends Activity {

	public static final String TAG = "MainActivity";
	/** 序列化测试
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	private final String objName = "/mnt/sdcard/object.txt";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("Intent 传递对象");
		setContentView(R.layout.activity_second);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		Person person = bundle.getParcelable("person");
		person.printPerson();
	}

	public void onBtnClick(View view) throws StreamCorruptedException,
			IOException, ClassNotFoundException {
		this.finish();
	}

}


 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android IntentAndroid系统中常用的组件之一,它用于在不同的组件之间传递消息和数据。在使用Intent时,常用的参数包括Action、Category、Data、Type和Extra等。其中,Extra是Intent中比较重要的一种参数,用于传递数据。 Extra的具体用法如下: 1. putExtra方法 使用putExtra方法向Intent中添加参数。putExtra方法有多个重载版本,可以传递不同类型的数据,如字符串、整数、布尔值、字符数组、字节数组等。例如: ``` Intent intent = new Intent(); intent.putExtra("name", "张三"); intent.putExtra("age", 18); startActivity(intent); ``` 2. getExtra方法 使用getExtra方法从Intent中获取参数。getExtra方法也有多个重载版本,需要根据传递数据的类型来选择相应的方法。例如: ``` Intent intent = getIntent(); String name = intent.getStringExtra("name"); int age = intent.getIntExtra("age", 0); ``` 其中,getIntExtra方法的第二个参数表示如果获取不到对应的值,则返回默认值0。 3. Bundle参数 使用Bundle参数可以在Intent传递更复杂的数据结构,如数组、集合等。例如: ``` Intent intent = new Intent(); Bundle bundle = new Bundle(); String[] names = {"张三", "李四", "王五"}; bundle.putStringArray("names", names); intent.putExtras(bundle); startActivity(intent); ``` 在接收方,使用getExtras方法获取Bundle参数,再根据键值获取对应的数据。例如: ``` Intent intent = getIntent(); Bundle bundle = intent.getExtras(); String[] names = bundle.getStringArray("names"); ``` 4. Parcelable参数 Parcelable参数可以用于在Intent传递自定义对象。自定义对象需要实现Parcelable接口,并重写相应的方法。例如: ``` public class Person implements Parcelable { private String name; private int age; // 省略构造方法和其他方法 @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); } public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() { @Override public Person createFromParcel(Parcel in) { return new Person(in); } @Override public Person[] newArray(int size) { return new Person[size]; } }; private Person(Parcel in) { name = in.readString(); age = in.readInt(); } @Override public int describeContents() { return 0; } } ``` 在使用Intent传递自定义对象时,需要使用putExtra方法,并将自定义对象作为参数传递。例如: ``` Intent intent = new Intent(); Person person = new Person("张三", 18); intent.putExtra("person", person); startActivity(intent); ``` 在接收方,使用getParcelableExtra方法获取Parcelable参数。例如: ``` Intent intent = getIntent(); Person person = intent.getParcelableExtra("person"); ``` 以上就是Android Intent中Extra参数的几种用法。在实际开发中,Extra参数可以用于在不同的Activity、Service和BroadcastReceiver之间传递数据,非常方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值