导航:首页 > 网络数据 > 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传大数据相关的资料

热点内容
身份证验证的js文件 浏览:994
什么人经常换微信名字 浏览:707
网站源码挖掘 浏览:944
荔枝fm和app哪个播放量好 浏览:535
乐秀音频在哪个文件里 浏览:486
以数据说话什么意思 浏览:319
java中对象 浏览:794
酷狗网络机顶盒如何设置 浏览:408
threejs添加文字 浏览:436
微信春节理财通 浏览:948
qq主题模块应用教程 浏览:993
javaweb项目打包 浏览:59
qq主题是哪个文件夹里 浏览:642
超越狂暴升级txt书包 浏览:263
cad复制不能粘贴到新建文件 浏览:565
linux网络socket编程 浏览:926
战争雷霆客户端在哪个文件里 浏览:847
如何授权点淘app读取步数 浏览:671
sonarqube代码负责度 浏览:116
step7左面编程框怎么调出来 浏览:348

友情链接