原文地址: http://www.jollen.org/blog/2009/01/jollen-android-programming-11.html

AndroidManifest.xml 是一個用來描述 Android 應用程式「整體資訊」的設定檔。簡單來說,這是一個「自我介紹」檔,我們可以向 Android 系統「介紹」我們的 Android 應用程式,以便讓 Android 系統完整地了解我們的應用程式資訊。

在 [教學, #9] 中,我們提及:「在這裡修改 AndroidManifest.xml 的目的是為了『在我們的 Android 應用程式裡加入一個 Service 類別』,這樣才有辦法啟動 Service...」這個工作的目的是為了向 Android 系統做二項自我介紹。說明如下。

1. 應用程式「實作了一個 MokoService 類別」

    <application android:icon="@drawable/icon" android:label="@string/app_name">    ...        <service android:name=".MokoService">        ...        </service>    ...    </application>

在 application 標籤裡加入 ‘service’ 標籤,告訴 Android 系統我們的應用程式有一個叫做「MokoService」的類別。「android:name」屬性用來指定 Service 的類別名稱,別忘了在 AndroidManifest.xml 裡,類別名稱都是以「.」(小數點)開始。

2. MokoService 類別可處理「com.moko.hello.START_MUSIC」意圖

       <service android:name=".MokoService">        <intent-filter>        <action android:name="com.moko.hello.START_MUSIC" />        <category android:name="android.intent.category.DEFAULT" />        </intent-filter>        </service>

在 service 標籤裡加入 ‘intent-filter’ 標籤,告訴 Android 系統我們的應用程式可「濾出」哪一個「Intent」。在前面的教學裡,我們把 Intent 暫時解釋為 Event(事件);因此,這裡的「自我介紹」用意是為了告訴 Android 系統,我們可接受的事件名稱為何。

我們只要在 intent-filter 標籤裡加入 ‘action’ 標籤,並指定 action 標籤的 android:name 屬性即可。Intent 的命名規則為「xxx.yyy.NAME」的路徑命名法。

當 Android 收到由 Activity 發出的 Intent 後,便去找尋可處理 com.moko.hello.START_MUSIC 的類別,然後載入並啟動此類別。

最後,在 ’intent-filter’ 裡加入 ‘category’ 標籤,用來定義 com.moko.hello.START_MUSIC 的分類,在這裡指定為預設類別 「android.intent.category.DEFAULT」,這是一個 Android 定義的常數。完整的 Service 類別「自我介紹」標籤與屬性,可參考 Android SDK 的說明。

--jollen

更多相关文章

  1. android 让Activity单例运行
  2. android
  3. Android程序怎么做单元测试
  4. android 环境的搭建
  5. Android技术内幕
  6. android 添加 iconv 支持
  7. Dialog窗口形式的Activity
  8. Android中配置单元测试
  9. android 工程库及引用

随机推荐

  1. android reboot重启分析
  2. 解决Intellij Idea,Android(安卓)Stuido的
  3. android读短信
  4. InputStream输入流,读取数据实例
  5. Android跳转intent简单教程
  6. Android(安卓)day_10-02 (广播接收者的使
  7. Android(安卓)自定义 Toast 显示时间
  8. android获取短信所有内容
  9. Android(安卓)WebView 图片自适应屏幕宽
  10. Android学习札记36:一个关于onSaveInstanc