android-Intent and IntentFilter

來源:互聯網
上載者:User

標籤:

一、Intent簡介

  Android使用Intent來封裝程式的調用“意圖”,Activity、Service、BroadcastReceiver三種重要的組件都是依靠Intent來啟動的,很明顯使用Intent提供了一致的編程模型;使用Intent還有一個好處是,在某些時候應用程式只是想啟動具有某些特徵組件,並不想和某個具體的組件耦合,如果使用形如startActivity(Class activityClass)的方法來啟動特定的組件,勢必造成一種寫入程式碼耦合,這樣也不利於高層次的解耦。

二、Intent啟動不同組建的方法

  Activity         :   startActivity(Intent intent) 

               startActivityForResult(Intent intent,int requestCode)

   Service        :   ComponentName startService(Intent Service)

               boolean bindService(Intent Service,ServiceConnection,int flags)

   BroadcastReceiver    :  sendBroadcast(Intent intent)

              sendBroadcast(Intent intent,String receiverPermission)

              sendOrderedBroadcast(Intent intent,String receiverPermission)

              sendOrderedBroadcast(Intent intent,String receiverPermission,BroadcastReceiver resultReceiver,Handler scheduler,

                          int initialCode,String initialData,Bundle initialExtras)

              sendStickyBroadcast(Intent intent)

              sendStickyBroadcast(Intent intent,String receiverPermission,BroadcastReceiver resultReceiver,Handler scheduler,

                          int initialCode,String initialData,Bundle initialExtras)

三、Intent對象的屬性

    Intent代表了Android應用的啟動“意圖”,Android應用將會根據Intent來啟動制定組件,至於具體啟動哪個組件取決於Intent的各個屬性。

    Intent屬性大致包括:Component、Action、Category、Data、Type、Extra 、Flag;

    1、Component屬性用於明確(顯示啟動)指定需要啟動的目標組件;

    2、Action代表該Intent所要完成的一個抽象動作,這個動作具體由哪個組件來完成則取決於Activity的<intent-filter.../>配置,其屬性的值是一個普通的字串;需要注意的是一個Intent對象最多隻能包括一個Action屬性,設定Action屬性的方法:setAction(MainActivity.KK_ACTION);。

    3、Category用於為Action增加額外的附加類別資訊,通常會與Action屬性結合使用,其屬性值也是一個普通的字串;一個Intent對象可以包括多個Category屬性,程式可調用Intent的addCategory(String str)方法為Intent添加該屬性。通過Intent調用Activity時,Android預設會自動添加CATEGORY_DEFAULT類別屬性,故在Filter配置中CATEGORY_DEFAULT是不可缺少的;

    4、

    5、

    6、Extra屬性用於“攜帶”需要交換的資料

    7、

  <intent-filter.../>元素裡通常包含如下子項目:

    0~N個<action.../>子項目;

    0~N個<category.../>子項目;

    0~1個<data.../>子項目。

四、Action、Category與intent-filter配置

  1、建立一個Android工程時設定檔AndroidManifest.xml中的intent-filter標籤中會自動產生兩個子項目:

    <action android:name="android.intent.action.MAIN" />和<category android:name="android.intent.category.LAUNCHER" />;那麼這兩個子項目代表什嗎?

  我們都知道一個應用程式可以有多個Activity,每個Activity是同層級的,那麼在啟動程式時,最先啟動哪個Activity呢?有些程式可能需要顯示在程式列表裡,有些不需要,該如何定義?首先,android.intent.action.MAIN決定應用程式最先啟動的Activity, 其次由android.intent.category.LAUNCHER決定應用程式是否顯示在程式列表裡,也就是說這兩者是配合使用的。

  2、為什麼加入android.intent.category.DEFAULT?

  每一個通過 startActivity() 方法發出的隱式 Intent 都至少有一個 category,就是 "android.intent.category.DEFAULT",所以只要是想接收一個隱式 Intent 的 Activity 都應該包括 "android.intent.category.DEFAULT" category,不然將導致 Intent 匹配失敗。一個 Intent 可以有多個 category,但至少會有一個,也是預設的一個 category。 只有 Intent 的所有 category 都匹配上,Activity 才會接收這個 Intent。

  3、getAction()與getCategories()

      String action=getIntent().getAction();   擷取該Activity對應的Intent的Action屬性
         Set<String> cates=getIntent().getCategories();  擷取該Activity對應的Intent的Category屬性

五、指定Action、Category調用系統Activity

  Intent代表了啟動某個程式組件的意圖,實際上Intent對象不僅可以啟動本應用內程式組件,也可以啟動Android系統的其他應用程式的組件,包括系統內建的程式組件(只要許可權允許)。Android內部提供了大量標準的Action、Category常量,其中用於啟動Activity的標準Action常量及對應的字串如下:

 

  上述Action、Category常量在java檔案中直接用即可,不需要在AndroidManifest.xml中配置。

六、data、Type屬性與intent-filter配置

 

android-Intent and IntentFilter

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.