导航:首页 > 网络数据 > androidbundle传大数据

androidbundle传大数据

发布时间:2023-09-25 20:59:18

❶ android bundle 如何传递自定义对象数组

在Android开发中,有时候需要从一个Activity中传递数据到另一个Activity中,在Bundle中已经封装好了简单数据类型,例如String ,int ,float等。但是如果我们想要传递一个复杂的数据类型,比如一个Book对象,该怎么办呢看

细的看了一下Bundle中的方法,其中有一个是putSerializable()方法,Serializable对象是一个可恢复对象接口,我们只需
要让Book对象实现Serializable接口,就可以使用Bundle.putSerializable()方法传递Book对象了。废话不多说
了,现将代码贴上:
Book类:
package com.bundletest.model.fneg;
import java.io.Serializable;
/**
*@Copyright:Copyright (c) 2008 - 2100
*@Company:Sagret
*@Author:fengcunhan [email protected]
*@Package:com.bundletest.model.fneg
*@FileName:Book.java
*@Time:2010-12-19
*@User:feng
*/
public class Book implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String id;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
实例化Book类,得到Book对象book以及设置成员变量:
if(TextUtils.isEmpty(bookName)||TextUtils.isEmpty(author)||TextUtils.isEmpty(id)){
Toast.makeText(AndroidBundleActivity.this, "输入框不能为空", Toast.LENGTH_SHORT).show();
}else{
Book book=new Book();
book.setName(bookName);
book.setAuthor(author);
book.setId(id);
Intent intent=new Intent(AndroidBundleActivity.this,RecieveActivity.class);
Bundle bundle=new Bundle();
bundle.putSerializable("book", book);
intent.putExtras(bundle);
startActivity(intent);
}
在另一个Activity中获取传递过来的book对象,并显示:
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
Book book=(Book)bundle.getSerializable("book");
nameText.setText("书名:"+book.getName());
authorText.setText("作者:"+book.getAuthor());
idText.setText("ID:"+book.getId());

❷ android intent跳转怎样传输大数据

在Activity或者组件之前传递信息时,一般采用intent绑定bundle的方式传值,但在使用过程中需要注意的是不要用bundle传递大容量数据:

在做项目的过程中,需要将听写界面的听写结果信息传递到听写记录界面供显示用,但是由于传递的数据量过大导致程序ANR,甚至直接报异常(传递的信息里面有bitmap转换成的byte数组、每一个词组的拼音、词语、语音信息),经过分析发现是由于bundle不能传递大容量的数据信息,在stackoverflow里面查阅发现有同行遇到类似的问题:

(1)“The size limit of Intent is still pretty low in Jelly Bean, which is somewhat lower than 1MB (around 90K), so you should always be cautious about your data length, even if your application targets only latest Android versions.”

(2)“As per my experience (sometime ago), you are able to put up to 1MB of data in a Bundleencapsulated inside Intent. I think, this restriction was valid up till Froyo or GingerBread.”

所以在通过bundle传递数据时只能传递较小的数据信息,对于在不同组件之间需要传递大容量数据的情况时,有几种替代方式可以解决不能用bundle传递这些数据的问题:

方法一:将需要传递的数据写在临时文件或者数据库中,再跳转到另外一个组件的时候再去读取这些数据信息,这种处理方式会由于读写文件较为耗时导致程序运行效率较低;

方法二:将需要传递的数据信息封装在一个静态的类中(注意当前组件和要跳转到的组件必须属于同一个进程,因为进程之间才能够共享数据),在当前组件中为类设置内容,然后再跳转到的组件中去取,这种处理方式效率很高,但是会破坏程序的独立性。

具体采用哪种替代方式取决于具体的情况,本人建议采取第二种处理方式,因为这样会大大提高程序的运行效率,至于程序的独立性,看你怎么去封装这个类了。

阅读全文

与androidbundle传大数据相关的资料

热点内容
ps3文件分割视频 浏览:280
微信图片一键转发软件 浏览:331
如何判断s200plc编程电缆 浏览:691
太原编程培训班哪个好 浏览:171
树叶吹奏教程 浏览:6
社交app带来了哪些社会问题 浏览:394
如何安装爱宝8800数据采集器 浏览:712
文件保存了怎么找不到了 浏览:476
彩票网站怎么辨真假 浏览:840
pr找不到该文件 浏览:963
java移除panel 浏览:354
jsp填充jsp 浏览:166
海关外贸大数据在哪里查 浏览:381
思特奇java笔试题 浏览:121
葫芦侠在手机中的文件名 浏览:813
plc编程应该怎么收钱 浏览:584
c语言中源文件由什么组成 浏览:890
linuxhttpdphp配置文件 浏览:607
拆单数据要怎么保存 浏览:17
mac电脑怎样压缩文件到100m 浏览:645

友情链接