2013年1月20日 星期日

[Android] Service notices

  • Service 會被開啟在和 caller 同個 process 中,並且執行在 main thread 中,所以若是要在 service 中執行耗時動作時,必須執行在 background thread 中。
  • 如果想讓 service 執行在另一個 process 中,則可在 AndroidManifest 中宣告 android:process=":process_description"
    • 此 service 有自己的分配空間。
    • a garbage collection, will not affect the user interface of your Activity。
    • 其中的 ":" 是表示此 service 為我們 application 私有的,若是沒有宣告,則表示是全域的而其他人也能存在到。
    • 當 service 執行在另外的 process,則需透過 interprocess communication (IPC) 與其溝通。


Binding to local service
  • If the Service is started in the same process as the Activity
    • The Activity can directly bind to the service a call of the bindService() method. This method has the ServiceConnection parameter.
    • The onServiceConnected() method is called on this object once the Service is available.
    • The Service return on its onBind() method an object of type IBinder which can be used to call to the service.



android.intent.action.BOOT_COMPLETED
  • 若 application 被安裝在 SD card 則接收不到 android.intent.action.BOOT_COMPLETED,因此必須再 register android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE。 (?)
    • If you application is installed on the SD card, then it is not available after the android.intent.action.BOOT_COMPLETED event. Register yourself in this case for the android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.
  • 在 Android 3.0,則是至少 user 開啟過 application 一次, application 才能接收到 android.intent.action.BOOT_COMPLETED。



* Reference
- Android Service Tutorial

沒有留言:

張貼留言