Android之Service設定android:process作用
在AndroidManifest.xml中定義service時會看到這樣的程式碼android:process=”:remote”,例如:
<service
android:name="com.test.hello.DaemonService"
android:enabled="true"
android:exported="false"
android:process=":remote" />
這個程式碼是什麼意思呢?
我們先來了解一下Service在AndroidManifest.xml中的宣告語法,其格式如下:
<service android:enabled=["true" | "false"]
android:exported=["true" | "false"]
android:icon="drawable resource"
android:isolatedProcess=["true" | "false"]
android:label="string resource"
android:name="string"
android:permission="string"
android:process="string" >
. . .
</service>
- android:exported:代表是否能被其他應用隱式呼叫,其預設值是由service中有無intent-filter決定的,如果有intent-filter,預設值為true,否則為false。為false的情況下,即使有intent-filter匹配,也無法開啟,即無法被其他應用隱式呼叫。
- android:name:對應Service類名
- android:permission:是許可權宣告
- android:process:是否需要在單獨的程式中執行,當設定為android:process=”:remote”時,代表Service在單獨的程式中執行。注意“:”很重要,它的意思是指要在當前程式名稱前面附加上當前的包名,所以“remote”和”:remote”不是同一個意思,前者的程式名稱為:remote,而後者的程式名稱為:App-packageName:remote。
- android:isolatedProcess :設定 true 意味著,服務會在一個特殊的程式下執行,這個程式與系統其他程式分開且沒有自己的許可權。與其通訊的唯一途徑是通過服務的API(bind and start)。
- android:enabled:是否可以被系統例項化,預設為 true因為父標籤 也有 enable 屬性,所以必須兩個都為預設值 true 的情況下服務才會被啟用,否則不會啟用。
1)預設情況(不指定process屬性)
1、一個app只執行在一個程式中,程式名字為包名。
2、一個service(所有元件都一樣)只作為一個執行緒執行在app的程式中,沒有自己獨立的程式。
2)設定android:process="xxxx"後
1、只要設定了這行程式碼,系統就會為service建立新的程式,service將執行在這個新的獨立的程式,它所在的apk依舊執行在原來程式。這樣就實現了Android使用多程式(用NDK的fork來做除外)
2、xxxx是自定義的,上面程式碼的remote是隨便寫的
3、當設定為 android:process = package:remote
將執行在package:remote程式中,屬於全域性程式,其他具有相同shareUID與簽名的APP可以跑在這個程式中。
4、當設定為 android:process = :remote
將執行在預設包名:remote程式中,而且是APP的私有程式,不允許其他APP的元件來訪問。
注意:a)<application> 和四大元件節點都可設定
b)設定<application android:process="xxx:xxx"> 可以指定app的程式名稱
c)若<application> 節點和四大元件都設定了android:process="xxx:xxxx"屬性,以元件的屬性為準
下面援引官方說明文件:
3)多程式引發的問題
靜態成員和單例失效:每個程式保持各自的靜態成員和單例,相互獨立。
執行緒同步機制失效:每個程式有自己的執行緒鎖。
SharedPreferences可靠性下降:不支援併發寫,會出現髒資料。
Application多次建立:不同程式跑在不同虛擬機器,每個虛擬機器啟動會建立自己的Application,自定義Application時生命週期會混亂。
綜上,不同程式擁有各自獨立的虛擬機器,Application,記憶體空間,由此引發一系列問題,因此在跨程式通訊開發時,要注意避開上述問題。
4)如何實現跨程式通訊?
相關文章
- Android之ServiceAndroid
- Android之android exported="false"作用AndroidExportFalse
- Android 之Service使用攻略Android
- 十四、Android效能優化之ServiceAndroid優化
- Android ServiceAndroid
- Android 四大元件之 ServiceAndroid元件
- Android 四大元件之" Service "Android元件
- Android Service SecurityAndroid
- Android Intent ServiceAndroidIntent
- Android Service 服務(一)—— ServiceAndroid
- Android 之 View透明度的設定AndroidView
- Android四大元件之Service篇Android元件
- Android總結篇系列:Android ServiceAndroid
- Android之背景圖片設定為重複Android
- Android Service完全解析Android
- 淺談 Android ServiceAndroid
- Android:Service詳解Android
- Android中Service概述Android
- android service詳解Android
- Android屬性設定android:noHistory="true"Android
- Talking about Android Initialization ProcessAndroid
- Android WebView ScrollBar設定AndroidWebView
- Android四大元件之Service,以及IntentServiceAndroid元件Intent
- Android知識點回顧之Service基礎Android
- Android探索之Service全面回顧及總結Android
- [android]android自動化測試三之設定AVD各項引數Android
- Android中的Web ServiceAndroidWeb
- Android Service詳解(二)Android
- Android Service詳解(一)Android
- Android中Service總結Android
- Android-Service詳解Android
- Android Service 訪問安全Android
- Android學習之四大元件(二)——serviceAndroid元件
- android設定桌面小部件Android
- oracle process設定的注意事情Oracle
- Android 程式生命週期 Process LifecycleAndroid
- Android Service最全面的解析Android
- android懸浮框(service形式)Android