Android中Intent組件的入門(mén)學(xué)習(xí)心得
什么是 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)似于中介、媒婆的角色。
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ì)話(huà)框,支持用戶(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_VIEW、ACTION_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é)
到此這篇關(guān)于Android中Intent組件入門(mén)學(xué)習(xí)的文章就介紹到這了,更多相關(guān)Android Intent組件學(xué)習(xí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
9個(gè)非常棒的Android代碼編輯器 移動(dòng)開(kāi)發(fā)者的最?lèi)?ài)
這篇文章主要為大家分享了9個(gè)非常棒的Android代碼編輯器,據(jù)說(shuō)這可是移動(dòng)開(kāi)發(fā)者的最?lèi)?ài),知道是哪九個(gè)Android代碼編輯器2015-12-12
Android九宮格手勢(shì)密碼代碼設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Android九宮格手勢(shì)密碼的代碼設(shè)計(jì)思路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android 跨進(jìn)程通Messenger(簡(jiǎn)單易懂)
這篇文章主要介紹了Android Messenger跨進(jìn)程通的相關(guān)資料,非常簡(jiǎn)單容易理解,對(duì)android messenger 進(jìn)程通訊的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-08-08
Android中實(shí)現(xiàn)淘寶購(gòu)物車(chē)RecyclerView或LIstView的嵌套選擇的邏輯
這篇文章主要介紹了Android中實(shí)現(xiàn)淘寶購(gòu)物車(chē)RecyclerView或LIstView的嵌套選擇的邏輯,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
Android開(kāi)發(fā)實(shí)現(xiàn)消除屏幕鎖的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)消除屏幕鎖的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android鎖屏的原理及消除屏幕鎖的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Android中EditText setText方法的踩坑實(shí)戰(zhàn)
這篇文章主要給大家分享了一些關(guān)于Android中EditText setText方法的踩坑記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
深入android Unable to resolve target ''android-XX''詳解
本篇文章是對(duì)android Unable to resolve target 'android-XX'錯(cuò)誤的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android實(shí)現(xiàn)手機(jī)監(jiān)控?cái)z像頭
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手機(jī)監(jiān)控?cái)z像頭,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android基于開(kāi)源項(xiàng)目xutils3實(shí)現(xiàn)下載
這篇文章主要介紹了Android基于開(kāi)源項(xiàng)目xutils3實(shí)現(xiàn)下載,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11

