Android進(jìn)程間使用Intent進(jìn)行通信
安卓使用Intent來封裝程序的“調(diào)用意圖”,使用Intent可以讓程序看起來更規(guī)范,更易于維護(hù)。
除此之外,使用Intent還有一個(gè)好處:有些時(shí)候我們只是想要啟動(dòng)具有某種特征的組件,并不想和某個(gè)具體的組件耦合,使用Intent在這種情況下有利于解耦。
Action,Category屬性與intent-filter配置
我們知道當(dāng)需要進(jìn)行Activity跳轉(zhuǎn)的時(shí)候需要在manifests.xml文件中配置Activity信息。其中主Activity還需要配置<intent-filter>,并且在標(biāo)簽中還要配置<action>和<category>兩個(gè)標(biāo)簽。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AcitvityTest"
tools:targetApi="31">
<activity android:name=".lifecycle.SecondActivity"/>
<activity
android:name=".lifecycle.FirstActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>其中Action代表該Intent所要完成的一個(gè)抽象“動(dòng)作”,而category則用于為Action增加額外的附加類別信息。通常Action屬性會(huì)與Category屬性結(jié)合使用。
<action>和<category>兩個(gè)標(biāo)簽中都可以指定android:name屬性,該屬性的值實(shí)際上就是字符串,<action>標(biāo)簽中的屬性表明該Activity能夠響應(yīng)哪些Intent。
<intent-filer>標(biāo)簽實(shí)際上就是IntentFilet對(duì)象,用于聲明該組件(比如Activity,Service,BroadcastReceiver)能夠滿足多少要求,每個(gè)組件可以聲明自己滿足多個(gè)Action要求,多個(gè)Category要求。只要某個(gè)組件能滿足的要求大于等于Intent所指定的要求,那么該Intent就能啟動(dòng)該組件。
一個(gè)Intent對(duì)象只能包含一個(gè)Action屬性,通過setAction(Stirng str)方法來進(jìn)行設(shè)置,一個(gè)Intent對(duì)象可以包含多個(gè)Category屬性,通過addCategory(String str)方法來進(jìn)行添加。
當(dāng)然,我們也可以通過設(shè)置Intent的Action和Category屬性來跳轉(zhuǎn)到系統(tǒng)的Activity
public class HomeActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button button = findViewById(R.id.home);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
});
}
}上述代碼中配置的Action和Category對(duì)應(yīng)的就是系統(tǒng)桌面,點(diǎn)擊按鈕后就會(huì)返回桌面。


到此這篇關(guān)于Android進(jìn)程間使用Intent進(jìn)行通信的文章就介紹到這了,更多相關(guān)Android Intent通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)疫情查詢app(實(shí)例代碼)
這篇文章主要介紹了用Android開發(fā)一個(gè)疫情查詢的APP,文中代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
Android?App實(shí)現(xiàn)閃屏頁廣告圖的全屏顯示實(shí)例
這篇文章主要為大家介紹了Android?App實(shí)現(xiàn)閃屏頁廣告圖的全屏顯示實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
android app跳轉(zhuǎn)應(yīng)用商店實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了android app跳轉(zhuǎn)應(yīng)用商店實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Android把商品添加到購物車的動(dòng)畫效果(貝塞爾曲線)
本篇文章介紹了Android把商品添加到購物車的動(dòng)畫效果,這個(gè)在很多項(xiàng)目中都用的到,具有一定的參考價(jià)值,有需要的可以了解一下。2016-10-10
Android 錄制手機(jī)屏幕視頻生成GIF圖片實(shí)例詳解
這篇文章主要介紹了Android 錄制手機(jī)屏幕視頻生成GIF圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android RecyclerView詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android RecyclerView詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android定制RadioButton樣式三種實(shí)現(xiàn)方法
三種方法實(shí)現(xiàn)Android定制RadioButton樣式:使用XML文件進(jìn)行定義/在JAVA代碼中定義等等,感興趣的朋友可以參考下,希望可以幫助到你2013-02-02
Android?Java?try?catch?失效問題及解決
這篇文章主要介紹了Android?Java?try?catch?失效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

