Activity與Service是否處於同一程式?
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.
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'sprocess
attribute can set a different default for all components. But component can override the defaultwith its ownprocess
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.
相關文章
- Activity與Service通訊的方式有三種:
- 判斷兩個ip地址是否屬於同一個網段程式碼
- Android中Service的啟動方式及Activity與Service的通訊方式Android
- Activity和Service跨程式通訊的兩種方式
- Application,Activity,Service的建立流程(1)APP
- Application,Activity,Service的建立流程(2)APP
- Service實時向Activity傳遞資料案例
- Android Service 通知Activity更新介面的方法研究Android
- Android程式啟動與Activity顯示Android
- 判斷ABAP程式碼是否處於update模式下執行的工具類模式
- Android 活動(activity)和服務(service)進行通訊Android
- Android service裡面啟動activity和alertdialogAndroid
- Service初探與非同步訊息處理機制非同步
- (原)同一種物料保稅與非保稅的處理
- 求救!!關於Web Service客戶端的程式,Web客戶端
- Fragment與Activity通訊Fragment
- Activity建立與跳轉
- 判斷一個時間是否處於指定的時間段內程式碼例項
- Windows Embedded for Point of Service 益處Windows
- 不同系統裡同一Customizing activity的顯示差異分析
- 在同一個Activity下實現兩個Fragment之間的通訊Fragment
- 舒服的判斷當前應用是否處於前臺
- 應用程式和Activity
- Android Activity/Service/Broadcaster三大元件之間互相呼叫AndroidAST元件
- 網路拓撲例項之交換機處於同一網路作為DHCP中繼與伺服器(八)中繼伺服器
- 關於SQL Server 2008是否是SQL Server 2005的一個Service PackSQLServer
- Android中程式與Service互動的方式——綜述Android
- Android中程式與Service互動的方式——總結Android
- Activity關於onConfigurationChanged方法
- 關於activity的生命週期1
- activity_main與fragment_mainAIFragment
- 謹慎處理 Service Worker 的更新
- 資料結構實驗六是否同一顆二叉樹資料結構二叉樹
- position:sticky定位元素是否在同一父元素中區別
- 關於"是否需要有程式碼規範"的個人看法
- 完全理解android Activity啟動模式LauchMode (深入Activity與任務棧)Android模式
- Laravel 啟用多個 work 對同一個佇列進行處理的時候是否會出現同步執行Laravel佇列
- 如何在CRM和C4C中用程式碼判斷當前是否處於configuration模式模式