「安卓開發(fā)指南」——(App組件)Intent和Intent過濾器

Intents and Intent Filters

Intent是一個用來從一個App組件啟動其他組件的消息對象。這里有3種基本的用法:

  • 啟動activity: 傳遞Intent,使用startActivity()或者startActivityForResult(),后者可以傳回結(jié)果。
  • 啟動service: 傳遞Intent,使用startService()啟動一個沒有用戶界面的service,如果service被設(shè)計為C/S結(jié)構(gòu),可以使用bindService()綁定一個service。
  • 傳遞broadcast: 可以向其他app傳遞信息通過Intent對象使用sendBroadcast()、 sendOrderedBroadcast()或sendStickyBroadcast()。

Intent類型

  • 顯式Intent 顯式的Intent類型就是在啟動的時候指定組件的類型,這個一般適用于自己的App。
  • 隱式Intent 隱式的Intent類型一般是指指明action,然后傳入Intent,適用于非己App。

當(dāng)你創(chuàng)建一個隱式的Intent對象,安卓系統(tǒng)會通過Intent過濾器查找manifest文件尋找合適的App打開之。如果很多應(yīng)用都有action,那么會出現(xiàn)一個對話框,讓用戶選擇想要的程序。

下面這張圖顯示了一個Activity如何通過Intent來打開另外一個Activity。

Intent

注意:為了保證你的app的安全性,請一直使用顯式的Intent來開啟一個service,并且不要打開Intent過濾器,因為通過過濾器打開service不能保證啟動者的身份。

建立一個Intent

Intent對象包含了安卓系統(tǒng)決定開啟那些應(yīng)用的信息并且包含了一些傳遞信息。Intent對象包含了以下內(nèi)容:

Component name: 要開啟的組件的名稱。顯式Intent所必須的,如果沒有該項,則為隱式Intent。對于service必須指定該項。你可以通過 setComponent(), setClass(), setClassName()等函數(shù)設(shè)置該項的內(nèi)容,比如com.example.ExampleActivity

Action 一個指定特定的行為的字符串,比如view、pick。
你可以使用setAction()函數(shù)來指定Intent的Action,也可以通過以方式自己定義action:

static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";

Data: URI對象可以作為MIME類型的數(shù)據(jù)。數(shù)據(jù)的類型由Intent的Action決定,例如,如果動作是ACTION_EDIT ,數(shù)據(jù)應(yīng)包含編輯文檔的URI。

設(shè)置Data URI需要調(diào)用setData()函數(shù),如果僅僅設(shè)置MIME類型,則需要調(diào)用setType()函數(shù),如果有必要,你可以同時設(shè)置兩者通過調(diào)用setDataAndType()函數(shù)(這種情況下setData()、setType()函數(shù)將不起作用)。

Category:這個內(nèi)容包含了額外的信息,這些信息需要下一個組件處理相關(guān)的信息,不是必要的。這兒有一些例子:

CATEGORY_BROWSABLE 目標(biāo)Activity允許自身通過web瀏覽器來顯示由鏈接諸如圖像或電子郵件消息中引用的數(shù)據(jù)來啟動。

CATEGORY_LAUNCHER 這個Activity是系統(tǒng)app啟動器中的任務(wù)欄里啟動的Activity。

你可以通過調(diào)用addCategory()函數(shù)來指定category。

Extras:鍵值對保存的信息??梢酝ㄟ^putExtra()存入,也可以建立一個Bundle對象保存所有的Extra數(shù)據(jù),然后通過putExtras()函數(shù)插入到Intent中。

Flags : Intent類中定義了Flags函數(shù)作為元數(shù)據(jù)。這個參數(shù)可以禁止安卓系統(tǒng)如何運行activity并且接下來如何執(zhí)行。相關(guān)函數(shù)為setFlags()

一個例子:

// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);

這個例子開啟了一個下載服務(wù),并將傳入的fileUrl內(nèi)容下載下來。

再來一個例子:

// Create the text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type

// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}

這個例子分享了一些文字信息。

強制使用App選擇器

有時候需要不直接打開默認(rèn)應(yīng)用,而是每次都要求用戶選擇打開的app,這可以創(chuàng)建一個createChooser() Intent對象,然后將其傳入startActivity(),如下:

Intent sendIntent = new Intent(Intent.ACTION_SEND);
...

// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show the chooser dialog
Intent chooser = Intent.createChooser(sendIntent, title);

// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}

接收一個隱式的Intent

需要使用intent-filter,前面有說,就不廢話了,直接上例子:

<activity android:name="MainActivity">
    <!-- This activity is the main entry, should appear in app launcher -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name="ShareActivity">
    <!-- This activity handles "SEND" actions with text data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
    <!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/vnd.google.panorama360+jpg"/>
        <data android:mimeType="image/*"/>
        <data android:mimeType="video/*"/>
    </intent-filter>
</activity>

這是一個多意圖的過濾器。第一個Activity,MainActivity,是app的主要入口,通過點擊圖標(biāo)進(jìn)入,這個是由"android.intent.action.MAIN"決定的。 "android.intent.category.LAUNCHER"決定這個Activity是在系統(tǒng)app啟動器中啟動的。如果沒有指定icon屬性,則圖標(biāo)為默認(rèn)。

第二個Activity,ShareActivity,為了便于共享文本和媒體內(nèi)容,可以通過ShareActivity從其他app中進(jìn)入,當(dāng)然需要通過過濾器。

如果你想只有自己訪問自己的過濾器而不是讓其他app訪問的話,可以設(shè)置exported屬性為false。

使用Pending Intent

pending Intent是Intent的一種包裝,主要的目的是將權(quán)限授予其他程序申請使用包含的Intent,就像執(zhí)行自己的程序一樣。主要的用途包括:

  • 申明一個Intent,當(dāng)用戶平臺接收到你的通知后執(zhí)行。
  • 申明一個Intent,當(dāng)用戶使用widget時執(zhí)行。
  • 申明一個Intent,在未來規(guī)定的時間內(nèi)執(zhí)行。

這里有三個方法:

  1. PendingIntent.getActivity() for an Intent that starts an Activity.
  2. PendingIntent.getService() for an Intent that starts a Service.
  3. PendingIntent.getBroadcast() for a Intent that starts an BroadcastReceiver.

具體的pending Intent官方解釋在這兒。

Intent解決方案

當(dāng)系統(tǒng)接收到一個隱式的Intent后,會通過action、data、category決定使用哪個app。

Action test

<intent-filter>
    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.VIEW" />
    ...
</intent-filter>

Category test

<intent-filter>
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    ...
</intent-filter>

Data test

<intent-filter>
    <data android:mimeType="video/mpeg" android:scheme="http" ... />
    <data android:mimeType="audio/mpeg" android:scheme="http" ... />
    ...
</intent-filter>

Intent匹配

queryIntentActivities()可以返回那些組件可以接受你的Intent,具體請查看這里


P.S:每天寫一篇感覺好累啊……

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容