日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Android中Intent組件的入門(mén)學(xué)習(xí)心得_Android

作者:dhkjzhr ? 更新時(shí)間: 2021-12-15 編程語(yǔ)言

什么是 Intent ?

Intent是Android開(kāi)發(fā)中一個(gè)非常重要且常用的類(lèi),Intent是一個(gè)消息傳遞對(duì)象,可以用來(lái)從其他應(yīng)用組件請(qǐng)求操作,使用Intent可以在一個(gè)組件中同一APP中的另一個(gè)組件或者是啟動(dòng)另一個(gè)APP的組件(這里所說(shuō)的組件包括Activity、Service和Broadcast)。

ctivity、service和broadcast receiver之間是通過(guò)Intent進(jìn)行通信的,而另外一個(gè)組件Content Provider本身就是一種通信機(jī)制,不需要通過(guò)Intent。我們來(lái)看下面這個(gè)圖就知道了:

如果Activity1需要和Activity2進(jìn)行聯(lián)系,二者不需要直接聯(lián)系,而是通過(guò)Intent作為橋梁。通俗來(lái)講,Intnet類(lèi)似于中介、媒婆的角色。

安卓官方對(duì)于Intent的介紹

Intent 的類(lèi)型?

Intent 有兩種類(lèi)型,分別是 顯式 Intent 和 隱式 Intent 。

顯式 Intent: 通過(guò)提供目標(biāo)應(yīng)用的軟件包名稱(chēng)或完全限定的組件類(lèi)名來(lái)指定可處理 Intent 的應(yīng)用。通常,您會(huì)在自己的應(yīng)用中使用 顯式 Intent 來(lái)啟動(dòng)組件,這是因?yàn)槟酪獑?dòng)的 Activity 或服務(wù)的類(lèi)名。例如,您可能會(huì)啟動(dòng)您應(yīng)用內(nèi)的新 Activity 以響應(yīng)用戶(hù)操作,或者啟動(dòng)服務(wù)以在后臺(tái)下載文件。

隱式 Intent: 不會(huì)指定特定的組件,而是聲明要執(zhí)行的常規(guī)操作,從而允許其他應(yīng)用中的組件來(lái)處理。例如,如需在地圖上向用戶(hù)顯示位置,則可以使用 隱式 Intent,請(qǐng)求另一具有此功能的應(yīng)用在地圖上顯示指定的位置。使用 隱式 Intent 時(shí),Android 系統(tǒng)通過(guò)將 Intent 的內(nèi)容與在設(shè)備上其他應(yīng)用的清單文件中聲明的 Intent 過(guò)濾器 進(jìn)行比較,從而找到要啟動(dòng)的相應(yīng)組件。如果 Intent 與 Intent 過(guò)濾器匹配,則系統(tǒng)將啟動(dòng)該組件,并向其傳遞 Intent 對(duì)象。如果多個(gè) Intent 過(guò)濾器兼容,則系統(tǒng)會(huì)顯示一個(gè)對(duì)話框,支持用戶(hù)選取要使用的應(yīng)用。

Intent 的組成

intent由組件名稱(chēng)(Component name)、操作(Action)、數(shù)據(jù)(Data)、類(lèi)別(Category)、額外的數(shù)據(jù)(Extra)和標(biāo)志(Flag)六個(gè)部分組成。

組件名稱(chēng) Component name:

組件名稱(chēng)是要啟動(dòng)的組件名稱(chēng)。如果使用的是 顯式 Intent 則必須指定此參數(shù),否則 Intent 無(wú)法識(shí)別要傳遞給哪個(gè)應(yīng)用組件。不指定此參數(shù)則為 隱式 Intent ,系統(tǒng)將根據(jù)其他 Intent 信息決定要接受 Intent 的應(yīng)用組件。如果想要啟動(dòng)特定的組件,則必須要指定此參數(shù)為該組件的名稱(chēng)。

操作 Action:

操作指定要執(zhí)行的通用操作的字符串。可以自定義自己的操作,但是通常應(yīng)該使用由 Intent 類(lèi)或其他框架類(lèi)定義的操作常量(例如 ACTION_VIEWACTION_SEND)。

數(shù)據(jù) Data:

數(shù)據(jù)是引用待操作數(shù)據(jù)或該數(shù)據(jù) MIME 類(lèi)型的 URI 對(duì)象。提供的數(shù)據(jù)類(lèi)型通常由 Intent 的操作決定。創(chuàng)建 Intent 時(shí),除了指定 URI 以外,指定數(shù)據(jù)類(lèi)型(其 MIME 類(lèi)型)往往也很重要。

類(lèi)別 Category:

類(lèi)別是一個(gè)包含應(yīng)處理 Intent 組件類(lèi)型的附加信息的字符串。可以將任意數(shù)量的類(lèi)別描述放入一個(gè) Intent 中,但大多數(shù) Intent 均不需要類(lèi)別。以下是一些常見(jiàn)類(lèi)別:

CATEGORY_BROWSABLE

目標(biāo) Activity 允許本身通過(guò)網(wǎng)絡(luò)瀏覽器啟動(dòng),以顯示鏈接引用的數(shù)據(jù),如圖像或電子郵件。

CATEGORY_LAUNCHER

此類(lèi)別描述了該 Activity 是任務(wù)的初始 Activity,當(dāng)系統(tǒng)啟動(dòng)時(shí)首先啟動(dòng)此 Activity。

額外的數(shù)據(jù) Extra:

攜帶完成請(qǐng)求操作所需的附加信息的鍵值對(duì)。正如某些操作使用特定類(lèi)型的數(shù)據(jù) URI 一樣,有些操作也使用特定的 extra。

標(biāo)志 Flag:

標(biāo)志在 Intent 類(lèi)中定義,充當(dāng) Intent 的元數(shù)據(jù)。標(biāo)志可以指示 Android 系統(tǒng)如何啟動(dòng) Activity(例如,Activity 應(yīng)屬于哪個(gè)任務(wù)),以及啟動(dòng)之后如何處理(例如,Activity 是否屬于最近的 Activity 列表)。

顯式 Intent 的使用

首先新建一個(gè) Activity 以及相應(yīng)的布局文件。

MyActivity

import androidx.annotation.Nullable;
import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myactivity);
    }
}

myactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"`在這里插入代碼片`
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyActivity">
    <TextView
        android:text="這是一個(gè)新的頁(yè)面"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

在清單文件 AndroidManifest.xml 中注冊(cè) MyActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication1">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication1">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MyActivity"/>
    </application>

</manifest>

此處android:name中使用 .MyActivity 是為了程序在運(yùn)行時(shí)直接將該 name 添加到 package 后,這樣方便程序去尋找此Activity并啟動(dòng),也可以將package復(fù)制添加到 android:name 中,例如<activity android:name="com.example.myapplication1.MyActivity"/> 。

在 MainActivity 中設(shè)定一個(gè)按鈕 id 為 startNew ,當(dāng)按下該按鈕時(shí)啟動(dòng) MyActivity 。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.startNew).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,MyActivity.class));
            }
        });
    }
}

此處在 new Intent(MainActivity.this,MyActivity.class) 中指明了要啟動(dòng)的 Activity 的名稱(chēng),所以為顯式 Intent 。

運(yùn)行效果:

隱式 Intent 的使用

首先在清單文件 AndroidManifest 中修改 MyAcitivity 的相關(guān)定義。

<activity
    android:name=".MyActivity"
    android:exported="true">
    <intent-filter>
        //為此intent定義一個(gè)新的action名稱(chēng)
        <action android:name="NewAction"/>
        //若指定category為DEFAULT,則指明此intent的行為方式是Activity
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

修改 MainActivity ,將原本的顯式 Intent 啟動(dòng)方式改為隱式 Intent ,即通過(guò)自定義的 action 名稱(chēng)來(lái)啟動(dòng)相對(duì)應(yīng)的 Activity 。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.startNew).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //此處使用NewAction調(diào)用MyActivity
                startActivity(new Intent("NewAction"));
            }
        });
    }
}

這樣就成功通過(guò)隱式 Intent 啟動(dòng) MyActivity 。

通常建議將 定義為 “包.intent.action.組件名” 。可以通過(guò)在組件中定義靜態(tài)變量來(lái)更加方便地訪問(wèn)該組件。例如,public static final String ACTION = "com.example.myapplication1.intent.action.MyActivity";

調(diào)用時(shí)直接使用 MyActivity.ACTION 即可。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.startNew).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //此處使用NewAction調(diào)用
                startActivity(new Intent(MyActivity.ACTION));
            }
        });
    }
}

運(yùn)行效果與顯式 Intent 相同。

通過(guò)隱式 Intent ,組件可以對(duì)不同APP之間的組件進(jìn)行訪問(wèn)。如果想讓本應(yīng)用的組件可以被其他的應(yīng)用進(jìn)行訪問(wèn),則需要對(duì) activity 標(biāo)簽加上一個(gè)屬性 android:exported ,當(dāng) android:exported 為 true 時(shí),本組件可以被其他應(yīng)用組件訪問(wèn),為 false 時(shí)則不可被其他應(yīng)用訪問(wèn),并彈出異常警告。可以在啟動(dòng)其他應(yīng)用組件時(shí)加上異常捕獲語(yǔ)句,例如,

public void onClick(View view) {
    try {
        startActivity(new Intent(MyActivity.ACTION));
    }catch (Exception e){
        Toast.makeText(MainActivity.this,"無(wú)法啟動(dòng)指定的Activity",Toast.LENGTH_SHORT).show();
    }
}

本文大部分參考了Android中對(duì)Intent部分的描述,希望本文對(duì)大家更好地使用Intent對(duì)象有所幫助。

總結(jié)

原文鏈接:https://blog.csdn.net/dhkjzhr/article/details/121880865

欄目分類(lèi)
最近更新