将包发送到其他进程

时间:2012-04-24 10:29:56

标签: android bundle serializable

我使用以下代码序列化对象并通过putSerializable将其附加到bundle,然后通过Message将bundle发送到其他进程。问题是我得到一个错误,该对象不可序列化。我尝试添加“implements Serialazable”,但我仍然得到同样的错误。

public static byte[] serializeObject(Object o)
{ 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    try { 
      ObjectOutput out = new ObjectOutputStream(bos); 
      out.writeObject(o); 
      out.close(); 

      // Get the bytes of the serialized object 
      byte[] buf = bos.toByteArray(); 

      return buf; 
    } catch(IOException ioe) { 
      Log.e("serializeObject", "error", ioe); 

      return null; 
    } 
  } 

这是拨打电话的代码:

                ArrayList<byte[]> blist=null;
                Bundle b = new Bundle();
                if (TriggerList != null && TriggerList.size() > 0)
                {
                    Iterator iter = TriggerList.iterator(); 
                    while (iter.hasNext()) 
                    {
                        Bundle entry = (Bundle) iter.next();
                        if (msg.arg1 == entry.getInt(ProjDefs.APP_ID))
                        {
                            if (blist == null)
                                blist=new ArrayList<byte[]>();
                            SerBundle sb = new SerBundle(entry);
                            byte[] bb = serializeObject(sb);
                            blist.add(bb);
                        }    
                    }
                    b.putSerializable(ProjDefs.SERIAL_DATA, blist);
                }
                NotifyClient(msg.arg1, ProjDefs.GET_APP_TRIGGERS_RESPONSE, 0, 0, b, null);

可序列化的类:

公共类SerBundle实现了Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
public Bundle bundle;

public SerBundle(Bundle bundle)
{
    this.bundle = bundle;
}

}

1 个答案:

答案 0 :(得分:0)

Bundle已经Parcelable。您可以将Bundle作为值放在另一个Bundle中,而不必使用ObjectOutputStream