欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

在不同Activity之间传递数据的四种常用方法

程序员文章站 2024-02-28 11:19:28
在android中传递数据的方法非常多,本次介绍4中比较常用的数据传递方法: 1.通过intent传递数据 2.通过静态变量(static)传递数据 3....

在android中传递数据的方法非常多,本次介绍4中比较常用的数据传递方法:

1.通过intent传递数据

2.通过静态变量(static)传递数据

3.通过剪贴板(clipboard)传递数据

4.通过全局对象传递数据

在transmitdataactivity.java中

package mobile.android.transmit.data;
public class transmitdataactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
button button1=(button)findviewbyid(r.id.button1);
button button2=(button)findviewbyid(r.id.button2);
button button3=(button)findviewbyid(r.id.button3);
button button4=(button)findviewbyid(r.id.button4);
button1.setonclicklistener(new buttonlistenner());
button2.setonclicklistener(new buttonlistenner());
button3.setonclicklistener(new buttonlistenner());
button4.setonclicklistener(new buttonlistenner());
}
class buttonlistenner implements onclicklistener{
@suppresslint("newapi")
@override
public void onclick(view view) {
intent intent=null;
switch (view.getid()) {
case r.id.button1:
intent=new intent(transmitdataactivity.this,myactivity1.class);
//保存string类型的值
intent.putextra("intent_string", "通过intent传递的字符串");
//保存integer类型的值
intent.putextra("intent_integer", 300);
data data=new data();
data.id=1000;
data.name="android";
//保存可序列化的对象
intent.putextra("intent_object", data);
//显示用于接收数据的activity
startactivity(intent);
break;
case r.id.button2:
intent=new intent();
intent.setclass(transmitdataactivity.this, myactivity2.class);
//下面的代码为myactivity2中放入三个静态变量赋值
myactivity2.id=3000;
myactivity2.name="保时捷";
myactivity2.data=new data();
myactivity2.data.id=5555;
myactivity2.name="android";
startactivity(intent);
break;
case r.id.button3:
intent=new intent(transmitdataactivity.this,myactivity3.class);
//获取剪贴板对象(clipboardmanager)
clipboardmanager clipboard=(clipboardmanager) getsystemservice(context.clipboard_service);
//创建data对象
data clipboarddata=new data();
//设置data对象中字段的值
clipboarddata.id=6666;
clipboarddata.name="通过clipboard传递的数据";
//创建字节数组输出流对象,用于将data对象转换为字节流
bytearrayoutputstream baos=new bytearrayoutputstream();
//用于保存data对象生成base64格式的字符串
string base64str="";
try {
objectoutputstream oos=new objectoutputstream(baos);
//将data对象写入对象输出流
oos.writeobject(clipboarddata);
//将字节流进行base64编码
base64str=base64.encodetostring(baos.tobytearray(), base64.default);
oos.close();
} catch (ioexception e) {
e.printstacktrace();
}
//获取存储文本数据的剪贴板数据对象(clipdata)
clipdata clipdata=clipdata.newplaintext("data", base64str);
//设置主剪贴板
clipboard.setprimaryclip(clipdata);
//显示myactivity3窗口
startactivity(intent);
break;
case r.id.button4:
//获取myapp对象
myapp myapp=(myapp) getapplicationcontext();
myapp.country="美国";
myapp.data.id=1234;
myapp.data.name="飞碟";
intent=new intent(transmitdataactivity.this,myactivity4.class);
startactivity(intent);
break;
default:
break;
}
}
}
}

data类:这个类是可序列化的,也就是实现了java.io.serializable接口的类。

package mobile.android.transmit.data;
import java.io.serializable;
public class data implements serializable{
public int id;
public string name;
}

在myactivity1.java中:package mobile.android.transmit.data;

public class myactivity1 extends activity{
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
setcontentview(r.layout.myactivity);
textview textview=(textview)findviewbyid(r.id.textview);
//获得string类型的值
string intentstring=getintent().getstringextra("intent_string");
//获得integer类型的值
int intentinteger=getintent().getextras().getint("intent_integer");
//获得data类型的值
data data=(data) getintent().getextras().get("intent_object");
stringbuffer sb=new stringbuffer();
sb.append("intent_string:");
sb.append(intentstring);
sb.append("\n");
sb.append("intent_integer:");
sb.append(intentinteger);
sb.append("\n");
sb.append("data.id:");
sb.append(data.id);
sb.append("\n");
sb.append("data.name:");
sb.append(data.name);
sb.append("\n");
//在屏幕上输出传递过来的值
textview.settext(sb.tostring());
}
}

在myactivity2.java中:

package mobile.android.transmit.data;
public class myactivity2 extends activity{
public static string name;
public static int id;
public static data data;
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
setcontentview(r.layout.myactivity);
textview textview=(textview)findviewbyid(r.id.textview);
stringbuffer sb=new stringbuffer();
sb.append("name:");
sb.append(name);
sb.append("\n");
sb.append("id:");
sb.append(id);
sb.append("\n");
sb.append("data.id:");
sb.append(data.id);
sb.append("\n");
sb.append("data.name:");
sb.append(data.name);
sb.append("\n");
textview.settext(sb.tostring());
}
}

在myactivity3中:

package mobile.android.transmit.data;
public class myactivity3 extends activity{
@suppresslint("newapi")
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
setcontentview(r.layout.myactivity);
textview textview=(textview)findviewbyid(r.id.textview);
//获取剪贴板对象
clipboardmanager clipboardmanager=(clipboardmanager) getsystemservice(context.clipboard_service);
//从剪贴板中获取base64编码字符串
string base64str=clipboardmanager.getprimaryclip().getitemat(0).gettext().tostring();
//将base64编码字符串编码成字节数组
byte[] buffer=base64.decode(base64str, base64.default);
bytearrayinputstream bais=new bytearrayinputstream(buffer);
try {
objectinputstream ois=new objectinputstream(bais);
//将字节流还原成data对象
data data=(data) ois.readobject();
//将base64编码原文和data对象字段值显示在textview控件中
textview.settext(base64str+"\n\ndata.id:"+data.id+"\ndata.name:"+data.name);
} catch (exception e) {
e.printstacktrace();
} 
}
}

在myactivity4.java中:

package mobile.android.transmit.data;
import android.app.activity;
import android.os.bundle;
import android.widget.textview;
public class myactivity4 extends activity{
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
setcontentview(r.layout.myactivity);
textview textview=(textview) findviewbyid(r.id.textview);
myapp myapp=(myapp) getapplicationcontext();
textview.settext("myapp.country:"+myapp.country+"\nmyapp.data.id:"+myapp.data.id+"\nmyapp.data.name:"+myapp.data.name);
}
}

关于在不同activity之间传递数据的四种常用方法小编就给大家介绍这么多,希望对大家有所帮助!