最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android進(jìn)程間使用Intent進(jìn)行通信

 更新時(shí)間:2023年02月28日 11:00:30   作者:haohulala  
Android進(jìn)程間通信(IPC,Inter-Process Communication)底層采用的是 Binder 機(jī)制,具體到應(yīng)用層有網(wǎng)友根據(jù)安卓四大組件將進(jìn)程間通信方式分為對(duì)應(yīng)的四種方式 Activity, Broadcast, ContentProvider, Service

安卓使用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)文章

最新評(píng)論

岳普湖县| 甘肃省| 卫辉市| 曲阜市| 南安市| 高青县| 安乡县| 弥勒县| 沁水县| 通城县| 偏关县| 二连浩特市| 延庆县| 望都县| 绥阳县| 洛川县| 蓬溪县| 宝山区| 清苑县| 冕宁县| 太康县| 唐河县| 金门县| 镇远县| 蛟河市| 苗栗市| 苗栗县| 临清市| 山阴县| 安远县| 万盛区| 确山县| 林周县| 嘉善县| 澎湖县| 乐昌市| 庄河市| 格尔木市| 北票市| 罗山县| 湘乡市|