如题,我想跨进程用广播传递复杂数据类型,比如ArrayList<Person> 想用intent.putParcelableArrayListExtra()函数传递过去,但是解析的时候报错了,求完整demo,谢谢大家
解决方案 »
- 关于android显示Sqlite数据库的问题,急求!!!
- 最新eclipse安装ADT报错,以及SDK manager相关问题。
- 问一个新手的问题,网上下载的Android程序怎么运行?
- android 用广播启动服务,在服务里启动activity,执行停止时第一次可以停止,后面不可以停。郁闷啊·····大侠帮帮忙
- 用bind的方法启动service,调用者退出后,service也销毁?
- Android 开发做游戏和做软件哪个更好?
- android新手 关于与服务器端交互的问题
- 现在android平台上实现gps获取经纬度有什么好方法?
- google定位问题
- 用AS开虚拟机,一直报错,虚拟机屏幕没反应,求帮忙
- Android WiFi porting
- 求SDK怎么安装不了啊,老是出现如下错误,好像是谷歌拒绝访问什么.xml的东西
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());
}
}
}