Develop系列-API Guides-應用組件-Intents and Intent Filters(1)

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   java   使用   

Intents 和 Intent Filters

(Intent譯為意圖,讓人比較費解,實際上解釋為“訊息”更加合理,乾脆就不翻譯了)

Intent是能在app組件間傳遞的訊息體,基本使用方式有如下三種:

  • 啟動activity
    • startActivity:intent描述需要啟動的activity和必須的資料
    • startActivityForResult:intent啟動的activity結束後,會觸發onActivityResult回調
  • 啟動服務
    • startService:intent描述需要啟動的service和必須的資料
    • bindService
  • 發送廣播
    • 通過Intent來的sendBroadcast(),sendOrderedBroadcast(), or sendStickyBroadcast()來發送廣播
Intent類型
  • 顯式intents:通過指定包名+類名來明確需要啟動的組件,一般用在app內部使用。
  • 隱式intents:不指定具體的組件,通過定義一些動作或者條件,在系統中匹配能夠執行動作或者滿足條件的組件。

  1. A建立intent,作為入參startActivity
  2. Android系統搜尋所有app的intent filter用於適配A發出的intent。(如果有多個匹配上,會彈框給使用者選擇)
  3. Android系統通過onCreate啟動匹配上的B,並把A的intent當做入參

Caution: 為了確保你的app是安全的,通常用顯式intent的方式來啟動service,因為用隱式intent啟動服務是有安全風險的,隱式intent無法預知啟動的service就是你想要的那個。

建立一個Intent

Intent主要包含如下屬性:

組件名

需要啟動的組件名,一般指包名+類名:com.example.ExampleActivity

對於顯式intent是必選的,對於隱式intent,不能指定。

Action(動作?)

 

Data(資料) Category(類別) Extras(額外資訊) Flags(標記) 顯式intent例子 隱式intent例子 應用程式選取器 接收隱式intent 過濾器例子 使用Pending Intent Intent解析 Action測試 Category測試 Data測試 Intent匹配

聯繫我們

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