日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

java数据传递给安卓_Android数据传递的五种方法汇总

發(fā)布時(shí)間:2024/10/6 Android 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java数据传递给安卓_Android数据传递的五种方法汇总 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android開發(fā)中,在不同模塊(如Activity)間經(jīng)常會(huì)有各種各樣的數(shù)據(jù)需要相互傳遞,我把常用的幾種 方法都收集到了一起。它們各有利弊,有各自的應(yīng)用場(chǎng)景。 我現(xiàn)在把它們集中到一個(gè)例子中展示,在例子中每一個(gè)按紐代表了一種實(shí)現(xiàn)方法。

1. 利用Intent對(duì)象攜帶簡(jiǎn)單數(shù)據(jù)

利用Intent的Extra部分來存儲(chǔ)我們想要傳遞的數(shù)據(jù),可以傳送int, long, char等一些基礎(chǔ)類型,對(duì)復(fù)雜的對(duì)象就無能為力了。

1.1 設(shè)置參數(shù)

//傳遞些簡(jiǎn)單的參數(shù)

Intent?intentSimple?=?new?Intent();

intentSimple.setClass(MainActivity.this,SimpleActivity.class);

Bundle?bundleSimple?=?new?Bundle();

bundleSimple.putString("usr",?"xcl");

bundleSimple.putString("pwd",?"zj");

intentSimple.putExtras(bundleSimple);

startActivity(intentSimple);

1.2?接收參數(shù)

this.setTitle("簡(jiǎn)單的參數(shù)傳遞例子");

//接收參數(shù)

Bundle?bunde?=?this.getIntent().getExtras();

String?eml?=?bunde.getString("usr");

String?pwd?=?bunde.getString("pwd");

2. 利用Intent對(duì)象攜帶如ArrayList之類復(fù)雜些的數(shù)據(jù)

這種原理是和上面一種是一樣的,只是要注意下。 在傳參數(shù)前,要用新增加一個(gè)List將對(duì)象包起來。

2.1?設(shè)置參數(shù)

//傳遞復(fù)雜些的參數(shù)

Map?map1?=?new?HashMap();

map1.put("key1",?"value1");

map1.put("key2",?"value2");

List>?list?=?new?ArrayList>();

list.add(map1);

Intent?intent?=?new?Intent();

intent.setClass(MainActivity.this,ComplexActivity.class);

Bundle?bundle?=?new?Bundle();

//須定義一個(gè)list用于在budnle中傳遞需要傳遞的ArrayList,這個(gè)是必須要的

ArrayList?bundlelist?=?new?ArrayList();

bundlelist.add(list);

bundle.putParcelableArrayList("list",bundlelist);

intent.putExtras(bundle);

startActivity(intent);

2.1 接收參數(shù)

?????this.setTitle("復(fù)雜參數(shù)傳遞例子");

//接收參數(shù)

Bundle?bundle?=?getIntent().getExtras();

ArrayList?list?=?bundle.getParcelableArrayList("list");

//從List中將參數(shù)轉(zhuǎn)回?List>

List>?lists=?(List>)list.get(0);

String?sResult?=?"";

for?(Map?m?:?lists)

{

for?(String?k?:?m.keySet())

{

sResult?+=?"\r\n"+k?+?"?:?"?+?m.get(k);

}

}

3. 通過實(shí)現(xiàn)Serializable接口

3.1 設(shè)置參數(shù)

利用Java語言本身的特性,通過將數(shù)據(jù)序列化后,再將其傳遞出去。

//通過Serializable接口傳參數(shù)的例子

HashMap?map2?=?new?HashMap();

map2.put("key1",?"value1");

map2.put("key2",?"value2");

Bundle?bundleSerializable?=?new?Bundle();

bundleSerializable.putSerializable("serializable",?map2);

Intent?intentSerializable?=?new?Intent();

intentSerializable.putExtras(bundleSerializable);

intentSerializable.setClass(MainActivity.this,

SerializableActivity.class);

startActivity(intentSerializable);

3.2 接收參數(shù)

this.setTitle("Serializable例子");

//接收參數(shù)

Bundle?bundle?=?this.getIntent().getExtras();

//如果傳?LinkedHashMap,則bundle.getSerializable轉(zhuǎn)換時(shí)會(huì)報(bào)ClassCastException,不知道什么原因

//傳HashMap倒沒有問題。

HashMap?map?=??(HashMap)bundle.getSerializable("serializable");

String?sResult?=?"map.size()?="+map.size();

Iterator?iter?=?map.entrySet().iterator();

while(iter.hasNext())

{

Map.Entry?entry?=?(Map.Entry)iter.next();

Object?key?=?entry.getKey();

Object?value?=?entry.getValue();

sResult?+="\r\n?key---->?"+(String)key;

sResult?+="\r\n?value---->?"+(String)value;

}

4. 通過實(shí)現(xiàn)Parcelable接口

這個(gè)是通過實(shí)現(xiàn)Parcelable接口,把要傳的數(shù)據(jù)打包在里面,然后在接收端自己分解出來。這個(gè)是Android獨(dú)有的,在其本身的源碼中也用得很多,

效率要比Serializable相對(duì)要好。

4.1 首先要定義一個(gè)類,用于?實(shí)現(xiàn)Parcelable接口

因?yàn)槠浔举|(zhì)也是序列化數(shù)據(jù),所以這里要注意定義順序要與解析順序要一致噢。

public?class?XclParcelable?implements?Parcelable?{

//定義要被傳輸?shù)臄?shù)據(jù)

public?int?mInt;

public?String?mStr;

public?HashMap?mMap?=?new?HashMap();

//Describe?the?kinds?of?special?objects?contained?in?this?Parcelable's?marshalled?representation.

public?int?describeContents()?{

return?0;

}

//Flatten?this?object?in?to?a?Parcel.

public?void?writeToParcel(Parcel?out,?int?flags)?{

//等于將數(shù)據(jù)映射到Parcel中去

out.writeInt(mInt);

out.writeString(mStr);

out.writeMap(mMap);

}

//Interface?that?must?be?implemented?and?provided?as?a?public?CREATOR?field

//that?generates?instances?of?your?Parcelable?class?from?a?Parcel.

public?static?final?Parcelable.Creator?CREATOR

=?new?Parcelable.Creator()?{

public?XclParcelable?createFromParcel(Parcel?in)?{

return?new?XclParcelable(in);

}

public?XclParcelable[]?newArray(int?size)?{

return?new?XclParcelable[size];

}

};

private?XclParcelable(Parcel?in)?{

//將映射在Parcel對(duì)象中的數(shù)據(jù)還原回來

//警告,這里順序一定要和writeToParcel中定義的順序一致才行!!!

mInt?=?in.readInt();

mStr??=?in.readString();

mMap??=?in.readHashMap(HashMap.class.getClassLoader());

}

public?XclParcelable()?{

//?TODO?Auto-generated?constructor?stub

}

}

4.2 ?設(shè)置參數(shù)

//通過實(shí)現(xiàn)Parcelable接口傳參的例子

Intent?intentParcelable?=?new?Intent();

XclParcelable?xp?=?new?XclParcelable();

xp.mInt?=?1;

xp.mStr?=?"字符串";

xp.mMap?=?new?HashMap();

xp.mMap.put("key",?"value");

intentParcelable.putExtra("Parcelable",?xp);

intentParcelable.setClass(MainActivity.this,

ParcelableActivity.class);

startActivity(intentParcelable);

4.3 接收參數(shù)

??????this.setTitle("Parcelable例子");

//接收參數(shù)

Intent?i?=?getIntent();

XclParcelable?xp?=?i.getParcelableExtra("Parcelable");

TextView??tv?=?(TextView)findViewById(R.id.tv);

tv.setText(??"?mInt?="+xp.mInt

+"\r\n?mStr"+xp.mStr

+"\r\n?size()="+xp.mMap.size());

5. 通過單例模式實(shí)現(xiàn)參數(shù)傳遞

單例模式的特點(diǎn)就是可以保證系統(tǒng)中一個(gè)類有且只有一個(gè)實(shí)例。這樣很容易就能實(shí)現(xiàn),

在A中設(shè)置參數(shù),在B中直接訪問了。這是幾種方法中效率最高的。

5.1 ?定義一個(gè)單實(shí)例的類

//單例模式

public?class?XclSingleton

{

//單例模式實(shí)例

private?static?XclSingleton?instance?=?null;

//synchronized?用于線程安全,防止多線程同時(shí)創(chuàng)建實(shí)例

public?synchronized?static?XclSingleton?getInstance(){

if(instance?==?null){

instance?=?new?XclSingleton();

}

return?instance;

}

final?HashMap?mMap;

private?XclSingleton()

{

mMap?=?new?HashMap();

}

public?void?put(String?key,Object?value){

mMap.put(key,value);

}

public?Object?get(String?key)

{

return?mMap.get(key);

}

}

5.2 設(shè)置參數(shù)

//通過單例模式傳參數(shù)的例子

XclSingleton.getInstance().put("key1",?"value1");

XclSingleton.getInstance().put("key2",?"value2");

Intent?intentSingleton?=?new?Intent();

intentSingleton.setClass(MainActivity.this,

SingletonActivity.class);

startActivity(intentSingleton);

5.3 接收參數(shù)

??????????this.setTitle("單例模式例子");

//接收參數(shù)

HashMap?map?=?XclSingleton.getInstance().mMap;

String?sResult?=?"map.size()?="+map.size();

//遍歷參數(shù)

Iterator?iter?=?map.entrySet().iterator();

while(iter.hasNext())

{

Map.Entry?entry?=?(Map.Entry)iter.next();

Object?key?=?entry.getKey();

Object?value?=?entry.getValue();

sResult?+="\r\n?key---->?"+(String)key;

sResult?+="\r\n?value---->?"+(String)value;

}

例子源碼放在:?下載

總結(jié)

以上是生活随笔為你收集整理的java数据传递给安卓_Android数据传递的五种方法汇总的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。