public Person() { super(); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeInt(age); dest.writeString(name); }
public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
@Override public Person[] newArray(int size) { // TODO Auto-generated method stub return new Person[size]; }
@Override public Person createFromParcel(Parcel source) { // TODO Auto-generated method stub return new Person(source.readInt(), source.readString()); } }; @Override public String toString() { return "Person [age=" + age + ", name=" + name + "]"; }
}sendBroadcast case R.id.menu_gesturelibrary: // Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:10086")); // startActivity(call); // showActivity();
Intent rece = new Intent(RECE); Person person = new Person(55, "C"); Person person2 = new Person(44, "B"); persons = new ArrayList<Person>(); persons.add(person); persons.add(person2); // Bundle bundle = new Bundle(); // bundle.putParcelableArrayList("person", persons); // rece.putExtra("persons", bundle); rece.putParcelableArrayListExtra("persons", persons); sendBroadcast(rece); package com.example.test;import java.util.ArrayList;import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle;import com.example.person.Person;public class TestRece extends BroadcastReceiver{
private static final String RECE = "com.example.person";
Persons类
package com.example.person;import android.os.Parcel;
import android.os.Parcelable;public class Person implements Parcelable{
private int age;
private String name;
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
public Person() {
super();
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeInt(age);
dest.writeString(name);
}
public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return new Person[size];
}
@Override
public Person createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new Person(source.readInt(), source.readString());
}
};
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
}sendBroadcast case R.id.menu_gesturelibrary:
// Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:10086"));
// startActivity(call);
// showActivity();
Intent rece = new Intent(RECE);
Person person = new Person(55, "C");
Person person2 = new Person(44, "B");
persons = new ArrayList<Person>();
persons.add(person);
persons.add(person2);
// Bundle bundle = new Bundle();
// bundle.putParcelableArrayList("person", persons);
// rece.putExtra("persons", bundle);
rece.putParcelableArrayListExtra("persons", persons);
sendBroadcast(rece);
package com.example.test;import java.util.ArrayList;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;import com.example.person.Person;public class TestRece extends BroadcastReceiver{
private static final String RECE = "com.example.person";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(RECE)){
System.out.println(RECE);
// Bundle bundle = intent.getBundleExtra("persons");
// if(bundle == null){
// System.out.println("null");
// }
// else{
// ArrayList<Person> person = (ArrayList<Person>) bundle.get("person");
// if(person == null){
// System.out.println("person null");
// }
// System.out.println(person.get(1).toString());
// }
ArrayList<Person> persons = intent.getParcelableArrayListExtra("persons");
if(persons == null || persons.size() == 0){
System.out.println("null");
}
else {
System.out.println(persons.get(0).toString());
}
}
}