前言
-
Intent
在Android
開發的應用非常常見 - 今天我就帶給大家簡單講一下
Intent
的相關知識 & 其用法
目錄
1. 定義
意圖,描述的是應用的動作 & 其對應的數據
2. 作用
- 指定當前組件要完成的動作
- 在
Android
不同組件間 傳遞數據
Activity
、Service
、BroadcastReceiver
之間的通信載體 =Intent
下面,將根據Intent
的作用,詳細講解其使用方法
3. 使用1:指定當前組件要完成的動作
該使用 分為顯式 & 隱式意圖:
3.1 顯式意圖
- 特點
明確指定需啟動的組件名
即 顯式
Intent
不需 解析Intent
則可直接啟動目標組件
具體使用
明確指定組件名的方式:調用Intent
的構造方法、Intent.setComponent()
、Intent.setClass()
實例說明
// 使FirstActivity啟動SecondActivity(通過按鈕)
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 1. 實例化顯式Intent & 通過構造函數接收2個參數
// 參數1 = Context:啟動活動的上下文,一般為當前Activity
// 參數2 = Class:是指定要啟動的目標活動
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
// 2. 通過Activity類的startActivity()執行該意圖操作(接收一個Intent對象)
// 將構建好的Intent對象傳入該方法就可啟動目標Activity
startActivity (intent);
}
});
3.2 隱式意圖
- 特點
無明確指定需啟動的組件名,但 指定了需啟動組件需滿足的條件
即 隱式
Intent
需 解析Intent
,才可啟動目標組件
- 具體使用
通過AndroidManifest.xml
文件下的<Activity>
標簽下的<intent -filter>
聲明 需 匹配的條件
一個
<Activity>
標簽下可以有多組<intent -filter>,只需匹配其中1組即可
- 詳細說明
聲明條件含:動作(Action
)、類型(Category
)、數據(Data
)
- 實例說明
// 使FirstActivity啟動SecondActivity(通過按鈕)
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 1. 實例化1個隱式Intent對象,并指定action參數
Intent intent = new Intent("android.intent.action.ALL_APPS");
// 2. 調用Intent中的addCategory()來添加一個category
// 注:每個Intent中只能指定1個action,但卻能指定多個category
intent.addCategory("com.example.intent_test.MY_CATEGORY");
startActivity (intent);
}
});
// 為使SecondActivity能繼續響應該Intent
// 我們需在AndroidManifest.xml文件下的<Activity>標簽下配置<intent -filter>的內容
<intent-filter >
<action android:name="android.intent.action.ALL_APPS"/>
<category android:name="android.intent.category.DEFAULT">
</category>
<category android:name="com.example.intent_test.MY_CATEGORY"/>
</intent-filter>
4. 使用2:不同組件間 傳遞數據
4.1 使用方法
putExtra()
、Bundle
方式
4.2 可傳遞的數據類型
a. 8種基本數據類型(boolean byte char short int long float double
)、String
b. Intent
、Bundle
c. Serializable
對象、Parcelable
及其對應數組、CharSequence
類型
d. ArrayList
,泛型參數類型為:<Integer>
、<? Extends Parcelable>
、<Charsequence>
、<String>
4.3 具體使用
在當前Activity
把要傳遞的數據暫存在Intent
中、在新啟動的Activity
中取出Intent中的數據
- 方法1:putExtra()
// 目的:將FristActivity中的一個字符串傳遞到SecondActivity中,并在SecondActivity中將Intent對象中的數據(FristActivity傳遞過來的數據)取出
// 1. 數據傳遞
// a. 創建Intent對象(顯示Intent)
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
// b. 通過putExtra()方法傳遞一個字符串到SecondActivity;
// putExtra()方法接收兩個參數:第一個是鍵,第二個是值(代表真正要傳遞的數據)
intent.putExtra("data","I come from FirstActivity");
// c. 啟動Activity
startActivity(intent);
// 2. 數據取出(在被啟動的Activity中)
// a. 獲取用于啟動SecondActivit的Intent
Intent intent = getIntent();
// b. 調用getStringExtra(),傳入相應的鍵名,就可得到傳來的數據
// 注意數據類型 與 傳入時保持一致
String data = intent.getStringExtra("data");
- 方法2:Bundle
// 1. 數據傳遞
// a. 創建Intent對象(顯示Intent)
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
// b. 創建bundle對象
Bundle bundle = new Bundle();
// c. 放入數據到Bundle
bundle.putString("name", "carson");
bundle.putInt("age", 28);
// d. 將Bundle放入到Intent中
intent.putExtras(bundle);
// e. 啟動Activity
startActivity(intent);
// 2. 數據取出(在被啟動的Activity中)
// a. 獲取用于啟動SecondActivit的Intent
Intent intent = getIntent();
// b. 通過Intent獲取bundle
Bundle bundle = intent.getExtras();
// c. 通過bundle獲取數據傳入相應的鍵名,就可得到傳來的數據
// 注意數據類型 與 傳入時保持一致
String nameString = bundle.getString("name");
int age = bundle.getInt("age");
4.4 兩種方式的區別
Bundle
意為 捆綁 的意思,更多適用于:
連續傳遞數據
若需實現連續傳遞:Activity A -> B -> C;若使用putExtra()
,則需寫兩次intent = A->B先寫一遍 + 在B中取出來 & 再把值重新寫到Intent中再跳到C;若使用Bundle
,則只需取出 & 傳入Bundle
對象即可可傳遞的值:對象
putExtra()
無法傳遞對象,而Bundle
則可通過putSerializable
傳遞對象
但傳遞的對象要實現Serializable接口
// 如傳遞User類的對象
public class User implements Serializable {
...
}
// 傳遞時
User user = new User();
Intent intent = new Intent(MyActivity.this,OthereActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("user", user);
intent.putExtras(bundle);
而 putExtra()
更多使用于單次傳遞、傳遞簡單數據類型的應用場景
5. 總結
- 本文對
Android
中的Intent組件進行了全面的介紹 - 接下來我將繼續介紹
Android
開發中的相關知識,感興趣的同學可以繼續關注本人博客Carson_Ho的開發筆記
相關系列文章閱讀
Carson帶你學Android:學習方法
Carson帶你學Android:四大組件
Carson帶你學Android:自定義View
Carson帶你學Android:異步-多線程
Carson帶你學Android:性能優化
Carson帶你學Android:動畫
歡迎關注Carson_Ho的簡書
不定期分享關于安卓開發的干貨,追求短、平、快,但卻不缺深度。