星期二, 8月 23, 2011

激活組件

Android應用程式筆記

四種組件中的三種組件activities、services和broadcast receivers可以受不同步訊息intent激活,intent與組件在運行時彼此綁定,不管這個組件是屬於你的應用程式或者其他應用程式。

intent由Intent類別產生,Intent類別定義激活特定組件或者特定類型組件的消息,一個intent可以是明確的也可以是隱含的。

對於activities與services,intent定義一些行為去激活,例如"view"或者"send"一些東西,且可能指定資料的URI去激活。例如intent傳遞要求給activity,讓activty顯示圖片或者開啟網頁。在一些案例中,你可以啟動activty接收結果,以此為例,這個activity也可用intent回傳結果,例如你可以發出一個intent讓使用者選個人聯絡人回傳給你,回傳的intent包含一個指向所選定聯絡人的URI。

對於broadcast receivers,intent簡單定義公告當作廣播,例如標明裝置電池為低電量的廣播只包含"battery is low"的已知操作字串。

其他組件型態,content provider,不會被intent激活。而從ContentResolver來的要求才會激活content provider。content resolver處理所有與content provider的直接交易,所以用provider執行交易的組件不需要呼叫ContentResolver方法。這留下一層content provider與組件之間要求訊息的抽象層。

以下有激活各個組件不同的方法:

  • 你可以傳遞intent到startActivity()或者startActivityForResult()(如果你希望activty回傳結果)來啟動activty(或給他一些新的任務去做)。
  • 你可以傳遞intent到startService()來啟動service(或給正在進行的service新指令)。或者你可以傳遞intent到bindService()來綁定service。
  • 你可以傳遞intent到sendBroadcast()、sendOrderedBroadcast()或者sendStickyBroadcast()來初始一個廣播。
  • 你可以呼叫ContentResolver上的query()執行查詢content provider。



沒有留言:

張貼留言