什麼時候加上android.intent.category.DEFAULT

來源:互聯網
上載者:User

 

1、要弄清楚這個問題,首先需要弄明白什麼是implicit(隱藏) intent什麼是explicit(明確) intent。

Explicit Intent明確的指定了要啟動的Acitivity ,比如以下Java代碼:
Intent intent= new Intent(this, B.class)

Implicit Intent沒有明確的指定要啟動哪個Activity ,而是通過設定一些Intent Filter來讓系統去篩選合適的Acitivity去啟動。

2、intent到底發給哪個activity,需要進行三個匹配,一個是action,一個是category,一個是data。

理論上來說,如果intent不指定category,那麼無論intent filter的內容是什麼都應該是匹配的。但是,如果是implicit intent,android預設給加上一個CATEGORY_DEFAULT,這樣的話如果intent filter中沒有android.intent.category.DEFAULT這個category的話,匹配測試就會失敗。所以,如果你的 activity支援接收implicit intent的話就一定要在intent filter中加入android.intent.category.DEFAULT。

例外情況是:android.intent.category.MAIN和android.intent.category.LAUNCHER的filter中沒有必要加入android.intent.category.DEFAULT,當然加入也沒有問題。

我們定義的activity如果接受implicit intent的話,intent filer就一定要加上android.intent.category.DEFAULT這個category。

二、

android.intent.category.LAUNCHER 具體有什麼作用?我在xml裡面加和不加 感覺沒什麼區別啊。誰能解釋下!

<activity android:name="APAct">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="URLAct">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

一個應用程式可以有多個Activity,每個Activity是同層級的,那麼在啟動程式時,最先啟動哪個Activity呢?有些程式可能需 要顯示在程式列表裡,有些不需要。怎麼定義呢?android.intent.action.MAIN決定應用程式最先啟動的Activity android.intent.category.LAUNCHER決定應用程式是否顯示在程式列表裡

因為你的程式可能有很多個activity
只要xml設定檔中有這麼一個intent-filter,而且裡面有這個launcher,那麼這個activity就是點擊程式時最先啟動並執行那個activity。

現在你只有一個activity,那麼加不加就沒有關係了。

用於模擬器啟動時設定為預設開啟為的activity

三、來自另一篇文章的解釋:

在寫 AndroidManifest.xml 的時候,一直沒有搞明白,什麼時候要給 Activityandroid.intent.category.DEFAULT 過濾器,現在才明白。

Android treats all implicit intents passed to startActivity() as if they contained at least one category: "android.intent.category.DEFAULT" (the
CATEGORY_DEFAULT constant). Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters

意思是說,每一個通過 startActivity() 方法發出的隱式 Intent 都至少有一個 category,就是 "android.intent.category.DEFAULT",所以只要是想接收一個隱式 Intent 的 Activity 都應該包括 "android.intent.category.DEFAULT" category,不然將導致 Intent 匹配失敗。

從上面的論述還可以獲得以下資訊:
1、一個 Intent 可以有多個 category,但至少會有一個,也是預設的一個 category。
2、只有 Intent 的所有 category 都匹配上,Activity 才會接收這個 Intent。

Android系統提供的類別

說明

ALTERNATIVE

Intent資料預設動作的一個可替換的執行方法

SELECTED_ALTERNATIVE

ALTERNATIVE類似,但替換的執行方法不是指定的,而是被解析出來

BROWSABLE

聲明Activity可以由瀏覽器啟動

DEFAULT

為Intent過濾器中定義的資料提供預設動作

HOME

裝置啟動後顯示的第一個Activity

LAUNCHER

在應用程式啟動時首先被顯示

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.