这里,服务和活动在不同的包中.可能是不同的应用.
解决方法 >首先序列化要传递的对象.>将序列化对象放在intent extras中.
>在接收端,只需获取序列化对象,然后对其进行反序列化.
说,
Employee employee = new Employee();
然后,
intent.putExtra("employee",serializeObject(employee));
接收时,
byte[] sEmployee = extras.getByteArray("employee");
employee =(Employee)deserializeObject(sEmployee);
仅供参考,
public static byte[] serializeObject(Object o) throws Exception,IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos); try { out.writeObject(o); // Get the bytes of the serialized object byte[] buf = bos.toByteArray(); return buf; } catch (IOException e) { Log.e(LOG_TAG,"serializeObject",e); throw new Exception(e); } finally { if (out != null) { out.close(); } }}public static Object deserializeObject(byte[] b) throws StreamCorruptedException,IOException,ClassNotFoundException,Exception { ObjectinputStream in = new ObjectinputStream( new ByteArrayinputStream(b)); try { Object object = in.readobject(); return object; } catch (Exception e) { Log.e(LOG_TAG,"deserializeObject",e); throw new Exception(e); } finally { if (in != null) { in.close(); } }}总结
以上是内存溢出为你收集整理的android – 如何在Service和Activity之间传递复杂数据?全部内容,希望文章能够帮你解决android – 如何在Service和Activity之间传递复杂数据?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)