[转]Jollen 的 Android 教學,#9: …
2014-01-15 12:06
267 查看
原文地址: http://www.jollen.org/blog/2009/01/jollen-android-programming-9.html
上一個課程裡,我們實作了一個 Service 的類別稱為 MokoService,現在我們想要在 Activity 裡載入並啟動
MokoService 類別,讓它可以在背景執行,請依以下步驟完成這個任務。。
修改 AndroidManifest.xml
在 Package Explorer 視窗裡找到目前 Android 專案的資訊描述檔,檔名是
AndroidManifest.xml。這是一個用來描述 Android 應用程式「整體資訊」的檔案,每個 Android
應用程式專案都會有一個。在這裡修改 Androidmanifest.xml 的目的是為了「在我們的 Android 應用程式裡加入一個
Service 類別」,這樣才有辦法啟動 Service。修改後的內容如下,紅色的部份是新增的描述:。
這是什麼意思呢?我們留待後續再做說明。接著只需要再加上一行程式碼,就能啟動 MokoService 類別了。
啟動 Service - startService()
回到 HelloM 類別,加入一行程式碼:
Activity 類別裡有一個 method 叫做 startService:
startService(Intent service)
呼叫 startService() 即可啟動一個 Service 類別,只是,startService()
的參數是一個「Intent」的型別,並不是所要啟動的類別名稱。「Intent」是一個很像「Event」的類別,後續我們再做比較精確的說明,在這裡,我們不如把
Intent 當成是 Event(事件)。
當程式送出 com.moko.hello.START_MUSIC 事件給 Android 時,Android
便去尋找能處理此事件的類別,然後啟動它。在這裡,能處理 com.moko.hello.START_MUSIC 事件的類別就是
MokoService,這個關係就是透過 AndroidManifest.xml 的設定實現的。
--jollen
上一個課程裡,我們實作了一個 Service 的類別稱為 MokoService,現在我們想要在 Activity 裡載入並啟動
MokoService 類別,讓它可以在背景執行,請依以下步驟完成這個任務。。
修改 AndroidManifest.xml
在 Package Explorer 視窗裡找到目前 Android 專案的資訊描述檔,檔名是
AndroidManifest.xml。這是一個用來描述 Android 應用程式「整體資訊」的檔案,每個 Android
應用程式專案都會有一個。在這裡修改 Androidmanifest.xml 的目的是為了「在我們的 Android 應用程式裡加入一個
Service 類別」,這樣才有辦法啟動 Service。修改後的內容如下,紅色的部份是新增的描述:。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.moko.hello" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".HelloMoko" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MokoService"> <intent-filter> <action android:name="com.moko.hello.START_MUSIC" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service> </application> </manifest>
這是什麼意思呢?我們留待後續再做說明。接著只需要再加上一行程式碼,就能啟動 MokoService 類別了。
啟動 Service - startService()
回到 HelloM 類別,加入一行程式碼:
public class HelloMoko extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startService(new Intent ("com.moko.hello.START_MUSIC")); } }
Activity 類別裡有一個 method 叫做 startService:
startService(Intent service)
呼叫 startService() 即可啟動一個 Service 類別,只是,startService()
的參數是一個「Intent」的型別,並不是所要啟動的類別名稱。「Intent」是一個很像「Event」的類別,後續我們再做比較精確的說明,在這裡,我們不如把
Intent 當成是 Event(事件)。
當程式送出 com.moko.hello.START_MUSIC 事件給 Android 時,Android
便去尋找能處理此事件的類別,然後啟動它。在這裡,能處理 com.moko.hello.START_MUSIC 事件的類別就是
MokoService,這個關係就是透過 AndroidManifest.xml 的設定實現的。
--jollen
相关文章推荐
- Jollen&nbsp;的&nbsp;Android&nbsp;教學,#8:&nbsp;沒有&nbsp;…
- android中的 gravity 和…
- 细节决定品质-&nbsp;android优化
- Android 开发人员必须掌握的&… 分类: Android开发 2014-05-30 10:54 51人阅读 评论(0) 收藏
- [原创]Android&nbsp;HAL实例学习-Jollen…
- Android&nbsp;中的&nbsp;Service&nbsp;全面…
- [原创]Android&nbsp;HAL实例学习-Jollen…
- &nbsp;android.app.SuperNotCalled…
- android上使用&nbsp;google&nbsp;map&nbsp;会遇到…
- [转]Jollen 的 Android 教學,#9: 啟動 Service - startService()
- Android&nbsp;开发人员必须掌握的&amp;…
- Android&nbsp;中的&nbsp;Service&nbsp;全面总结
- android 程序运行报:"0x****"指令引用的"0x****"内存,该内存不能为"written"错误的解决
- Android Button、ImageButto…
- <摘>IOS、Android自动化测试框架Appium概述
- Android开发:调试出现“Source&nbsp;not…
- android 自带的主题 theme 的使用
- android getResources的作用
- android:&nbsp;源码模块自定义
- 变量声明是如果不使用 var&nb…