Android中Intent組件的入門學(xué)習(xí)心得
什么是 Intent ?
Intent是Android開發(fā)中一個非常重要且常用的類,Intent是一個消息傳遞對象,可以用來從其他應(yīng)用組件請求操作,使用Intent可以在一個組件中同一APP中的另一個組件或者是啟動另一個APP的組件(這里所說的組件包括Activity、Service和Broadcast)。
ctivity、service和broadcast receiver之間是通過Intent進(jìn)行通信的,而另外一個組件Content Provider本身就是一種通信機(jī)制,不需要通過Intent。我們來看下面這個圖就知道了:

如果Activity1需要和Activity2進(jìn)行聯(lián)系,二者不需要直接聯(lián)系,而是通過Intent作為橋梁。通俗來講,Intnet類似于中介、媒婆的角色。
Intent 的類型?
Intent 有兩種類型,分別是 顯式 Intent 和 隱式 Intent 。
顯式 Intent: 通過提供目標(biāo)應(yīng)用的軟件包名稱或完全限定的組件類名來指定可處理 Intent 的應(yīng)用。通常,您會在自己的應(yīng)用中使用 顯式 Intent 來啟動組件,這是因?yàn)槟酪獑拥?Activity 或服務(wù)的類名。例如,您可能會啟動您應(yīng)用內(nèi)的新 Activity 以響應(yīng)用戶操作,或者啟動服務(wù)以在后臺下載文件。
隱式 Intent: 不會指定特定的組件,而是聲明要執(zhí)行的常規(guī)操作,從而允許其他應(yīng)用中的組件來處理。例如,如需在地圖上向用戶顯示位置,則可以使用 隱式 Intent,請求另一具有此功能的應(yīng)用在地圖上顯示指定的位置。使用 隱式 Intent 時,Android 系統(tǒng)通過將 Intent 的內(nèi)容與在設(shè)備上其他應(yīng)用的清單文件中聲明的 Intent 過濾器 進(jìn)行比較,從而找到要啟動的相應(yīng)組件。如果 Intent 與 Intent 過濾器匹配,則系統(tǒng)將啟動該組件,并向其傳遞 Intent 對象。如果多個 Intent 過濾器兼容,則系統(tǒng)會顯示一個對話框,支持用戶選取要使用的應(yīng)用。
Intent 的組成
intent由組件名稱(Component name)、操作(Action)、數(shù)據(jù)(Data)、類別(Category)、額外的數(shù)據(jù)(Extra)和標(biāo)志(Flag)六個部分組成。
組件名稱 Component name:
組件名稱是要啟動的組件名稱。如果使用的是 顯式 Intent 則必須指定此參數(shù),否則 Intent 無法識別要傳遞給哪個應(yīng)用組件。不指定此參數(shù)則為 隱式 Intent ,系統(tǒng)將根據(jù)其他 Intent 信息決定要接受 Intent 的應(yīng)用組件。如果想要啟動特定的組件,則必須要指定此參數(shù)為該組件的名稱。
操作 Action:
操作指定要執(zhí)行的通用操作的字符串??梢宰远x自己的操作,但是通常應(yīng)該使用由 Intent 類或其他框架類定義的操作常量(例如 ACTION_VIEW、ACTION_SEND)。
數(shù)據(jù) Data:
數(shù)據(jù)是引用待操作數(shù)據(jù)或該數(shù)據(jù) MIME 類型的 URI 對象。提供的數(shù)據(jù)類型通常由 Intent 的操作決定。創(chuàng)建 Intent 時,除了指定 URI 以外,指定數(shù)據(jù)類型(其 MIME 類型)往往也很重要。
類別 Category:
類別是一個包含應(yīng)處理 Intent 組件類型的附加信息的字符串??梢詫⑷我鈹?shù)量的類別描述放入一個 Intent 中,但大多數(shù) Intent 均不需要類別。以下是一些常見類別:
CATEGORY_BROWSABLE:
目標(biāo) Activity 允許本身通過網(wǎng)絡(luò)瀏覽器啟動,以顯示鏈接引用的數(shù)據(jù),如圖像或電子郵件。
CATEGORY_LAUNCHER:
此類別描述了該 Activity 是任務(wù)的初始 Activity,當(dāng)系統(tǒng)啟動時首先啟動此 Activity。
額外的數(shù)據(jù) Extra:
攜帶完成請求操作所需的附加信息的鍵值對。正如某些操作使用特定類型的數(shù)據(jù) URI 一樣,有些操作也使用特定的 extra。
標(biāo)志 Flag:
標(biāo)志在 Intent 類中定義,充當(dāng) Intent 的元數(shù)據(jù)。標(biāo)志可以指示 Android 系統(tǒng)如何啟動 Activity(例如,Activity 應(yīng)屬于哪個任務(wù)),以及啟動之后如何處理(例如,Activity 是否屬于最近的 Activity 列表)。
顯式 Intent 的使用
首先新建一個 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="這是一個新的頁面"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
在清單文件 AndroidManifest.xml 中注冊 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)行時直接將該 name 添加到 package 后,這樣方便程序去尋找此Activity并啟動,也可以將package復(fù)制添加到 android:name 中,例如<activity android:name="com.example.myapplication1.MyActivity"/> 。
在 MainActivity 中設(shè)定一個按鈕 id 為 startNew ,當(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) 中指明了要啟動的 Activity 的名稱,所以為顯式 Intent 。
運(yùn)行效果:


隱式 Intent 的使用
首先在清單文件 AndroidManifest 中修改 MyAcitivity 的相關(guān)定義。
<activity
android:name=".MyActivity"
android:exported="true">
<intent-filter>
//為此intent定義一個新的action名稱
<action android:name="NewAction"/>
//若指定category為DEFAULT,則指明此intent的行為方式是Activity
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
修改 MainActivity ,將原本的顯式 Intent 啟動方式改為隱式 Intent ,即通過自定義的 action 名稱來啟動相對應(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"));
}
});
}
}
這樣就成功通過隱式 Intent 啟動 MyActivity 。
通常建議將 定義為 “包.intent.action.組件名” ??梢酝ㄟ^在組件中定義靜態(tài)變量來更加方便地訪問該組件。例如,public static final String ACTION = "com.example.myapplication1.intent.action.MyActivity";
調(diào)用時直接使用 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 相同。
通過隱式 Intent ,組件可以對不同APP之間的組件進(jìn)行訪問。如果想讓本應(yīng)用的組件可以被其他的應(yīng)用進(jìn)行訪問,則需要對 activity 標(biāo)簽加上一個屬性 android:exported ,當(dāng) android:exported 為 true 時,本組件可以被其他應(yīng)用組件訪問,為 false 時則不可被其他應(yīng)用訪問,并彈出異常警告??梢栽趩悠渌麘?yīng)用組件時加上異常捕獲語句,例如,
public void onClick(View view) {
try {
startActivity(new Intent(MyActivity.ACTION));
}catch (Exception e){
Toast.makeText(MainActivity.this,"無法啟動指定的Activity",Toast.LENGTH_SHORT).show();
}
}
本文大部分參考了Android中對Intent部分的描述,希望本文對大家更好地使用Intent對象有所幫助。
總結(jié)
到此這篇關(guān)于Android中Intent組件入門學(xué)習(xí)的文章就介紹到這了,更多相關(guān)Android Intent組件學(xué)習(xí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
9個非常棒的Android代碼編輯器 移動開發(fā)者的最愛
這篇文章主要為大家分享了9個非常棒的Android代碼編輯器,據(jù)說這可是移動開發(fā)者的最愛,知道是哪九個Android代碼編輯器2015-12-12
Android 跨進(jìn)程通Messenger(簡單易懂)
這篇文章主要介紹了Android Messenger跨進(jìn)程通的相關(guān)資料,非常簡單容易理解,對android messenger 進(jìn)程通訊的相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-08-08
Android中實(shí)現(xiàn)淘寶購物車RecyclerView或LIstView的嵌套選擇的邏輯
這篇文章主要介紹了Android中實(shí)現(xiàn)淘寶購物車RecyclerView或LIstView的嵌套選擇的邏輯,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12
Android開發(fā)實(shí)現(xiàn)消除屏幕鎖的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)消除屏幕鎖的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android鎖屏的原理及消除屏幕鎖的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Android中EditText setText方法的踩坑實(shí)戰(zhàn)
這篇文章主要給大家分享了一些關(guān)于Android中EditText setText方法的踩坑記錄,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
深入android Unable to resolve target ''android-XX''詳解
本篇文章是對android Unable to resolve target 'android-XX'錯誤的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android實(shí)現(xiàn)手機(jī)監(jiān)控攝像頭
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手機(jī)監(jiān)控攝像頭,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Android基于開源項(xiàng)目xutils3實(shí)現(xiàn)下載
這篇文章主要介紹了Android基于開源項(xiàng)目xutils3實(shí)現(xiàn)下載,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11

