Android之Intent附加數(shù)據(jù)的兩種實現(xiàn)方法
本文實例講述了Android之Intent附加數(shù)據(jù)的兩種實現(xiàn)方法。分享給大家供大家參考。具體如下:
第一種寫法,用于批量添加數(shù)據(jù)到Intent:
Intent intent = new Intent();
Bundle bundle = new Bundle();
//該類用作攜帶數(shù)據(jù)
bundle.putString("name", "林計欽");
intent.putExtras(bundle);
//為意圖追加額外的數(shù)據(jù),意圖原來已經(jīng)具有的數(shù)據(jù)不會丟失,但key同名的數(shù)據(jù)會被替換
第二種寫法:這種寫法的作用等價于上面的寫法,只不過這種寫法是把數(shù)據(jù)一個個地添加進Intent,這種寫法使用起來比較方便,而且只需要編寫少量的代碼。
Intent intent = new Intent();
intent.putExtra("name", "林計欽");
Intent提供了各種常用類型重載后的putExtra()方法,如: putExtra(String name, String value)、 putExtra(String name, long value),在putExtra()方法內(nèi)部會判斷當前Intent對象內(nèi)部是否已經(jīng)存在一個Bundle對象,如果不存在就會新建Bundle對象,以后調(diào)用putExtra()方法傳入的值都會存放于該Bundle對象,下面是Intent的putExtra(String name, String value)方法代碼片斷:
public class Intent implements Parcelable {
private Bundle mExtras;
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
}
希望本文所述對大家的Android程序設(shè)計有所幫助。
- Android 使用Intent傳遞數(shù)據(jù)的實現(xiàn)思路與代碼
- Android開發(fā)中Intent傳遞對象的方法分析
- Android中Intent傳遞對象的兩種方法Serializable,Parcelable
- Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的方法
- Android編程使用Intent傳遞對象的方法分析
- android中intent傳遞list或者對象的方法
- Android系列之Intent傳遞對象的幾種實例方法
- Android開發(fā)之利用Intent實現(xiàn)數(shù)據(jù)傳遞的方法
相關(guān)文章
解決Android studio 3.6.1 出現(xiàn)Cause: unable to find valid certifi
這篇文章主要介紹了Android studio 3.6.1 出現(xiàn)Cause: unable to find valid certification path to requested target 報錯的問題及解決方法,需要的朋友可以參考下2020-03-03
Android基于ibeacon實現(xiàn)藍牙考勤功能
這篇文章主要為大家詳細介紹了Android基于ibeacon實現(xiàn)藍牙考勤功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
Input系統(tǒng)截斷策略的分析與應(yīng)用詳解
這篇文章主要為大家介紹了Input系統(tǒng)截斷策略的分析與應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Android 實時監(jiān)測(監(jiān)聽)網(wǎng)絡(luò)連接狀態(tài)變化
這篇文章主要介紹了Android 實時監(jiān)測(監(jiān)聽)網(wǎng)絡(luò)連接狀態(tài)變化的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-06-06
Android Studio提示inotify大小不足的解決辦法
大家在使用Android Studio導(dǎo)入AOSP源碼的時候,可能會遇到inotify大小不足的問題,這篇文章就給大家介紹了怎么解決這個問題的方法,有需要的朋友們可以參考借鑒。2016-09-09

