Activity與Service是否處於同一程式?

yangxi_001發表於2013-11-18

     By default, all components of the same application run in the same process and most applications should not change this. However, if you find that you need to control which process a certain component belongs to, you can do so in the manifest file.
The manifest entry for each type of component element—<activity>, <service>, <receiver>, and <provider>—supports an android:process attribute that can specify a process in which that component should run.        

      一般來說:同一個包內的activity和service,如果service沒有設定屬性android:process=":remote"的話,service會和activity跑在同一個程式中,由於一個程式只有一個UI執行緒,所以,service和acitivity就是在同一個執行緒裡面的。android:process=":remote"值得注意他的用法!!!如果Activity想訪問service中的物件或方法,如果service設定屬性android:process=":remote",那麼就是跨程式訪問,跨程式訪問容易出現意想不到的問題,還是慎重給service設定屬性android:process=":remote"


再看下官方網頁上的說明:

1、http://developer.android.com/reference/android/app/Service.html

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding <service> declaration in its package's AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().

Note that services, like other application objects, run in the main thread of their hosting process.This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.

在預設情況下,service是與activity處於同一個程式中並且執行在UI執行緒中。

If we want to make this service run in a remote process (instead of the standard one for its .apk), we can useandroid:process in its manifest tag to specify one:

<service android:name=".app.MessengerService"
        android:process=":remote" />

Note that the name "remote" chosen here is arbitrary, and you can use other names if you want additional processes. The ':' prefix appends the name to your package's standard process name.

2、http://developer.android.com/intl/zh-CN/guide/topics/manifest/service-element.html

android:process
The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The<application> element's process attribute can set a different default for all components. But component can override the defaultwith its own process attribute, allowing you to spread your application across multiple processes.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process.If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so.This allows components in different applications to share a process, reducing resource usage.

如果以“:”開頭,代表在應用程式裡,當需要該service時,會自動建立新的程式。而如果是android:process="remote",沒有“:”分號的,則建立全域性程式,不同的應用程式共享該程式。



相關文章