如题,我想跨进程用广播传递复杂数据类型,比如ArrayList<Person> 想用intent.putParcelableArrayListExtra()函数传递过去,但是解析的时候报错了,求完整demo,谢谢大家

解决方案 »

  1.   

    你要写一个类实现parcelable,将数据打包在这个类里面。然后传值使用parcel~~~
      

  2.   

    楼主可以试试这样行不:自定义一个类比如UserData实现序列化接口,在里面定义一个变量List<Person>设置该变量的get&set
      

  3.   

    intent传递对象只能传递基本类型,对于传递对象必须将对象实现序列化接口
      

  4.   

    可以参考这里 http://whao189.iteye.com/blog/1056322
      

  5.   

     对不起各位,我没有说清楚,我的程序做过序列化了,但是只能在同一个app下传递,不能跨进程,希望大家给出意见或者建议。我用的是广播,跨进程传递复杂数据.
      

  6.   

    你可以用application来共享数据啊,不是非得用intent来传啊
      

  7.   

    想要传递自定义引用类型的数据,被传递数据类型(Person)需要进行序列化,你可以用Parcelable接口,借用AIDL文件,AIDL的使用方法可以搜一下有很多例子,不过要求你要交互的那个app也要拷贝你这个AIDL,这样才行
      

  8.   

    不同进程间通信需要用到aidl,你光光实现序列化是不够的,还需要创建该pojo的aidl
      

  9.   

    我知道aidl可以,但是我现在就打算用广播传递,希望大家给出意见或者建议,单单序列化是不够的我也知道,但是我不知道还差什么
      

  10.   

    没发现有啥问题,我这边可以运行的~接收到后可以解析的~
    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());
    }
    }
    }