
本文的合集已經(jīng)編著成書,高級Android開發(fā)強(qiáng)化實(shí)戰(zhàn),歡迎各位讀友的建議和指導(dǎo)。在京東即可購買:https://item.jd.com/12385680.html

在Android系統(tǒng)中, 進(jìn)程非常重要, 除了主進(jìn)程運(yùn)行App, 我們還可以使用其他進(jìn)程處理獨(dú)立任務(wù).進(jìn)程, 即Process. 進(jìn)程間通信, 即IPC(Inter-Process Communication).
在Android中, 使用多進(jìn)程只有一種方式, 在AndroidManifest中, 為四大組件(Activity, Service, Receiver, ContentProvider)指定android:process屬性.
<service
android:name=".PedometerCounterService"
android:exported="false"
android:process=":cy_pedometer_set"/>
exported="false"表示只與本應(yīng)用內(nèi)的進(jìn)程通信, 即包名相同.
默認(rèn)進(jìn)程的進(jìn)程名是包名.
? ~ adb shell ps | grep wangchenlong.chunyu.me.android_pedometer_set
u0_a354 28490 410 2259024 80272 ffffffff 00000000 S wangchenlong.chunyu.me.android_pedometer_set
u0_a354 28515 410 2191112 60080 ffffffff 00000000 S wangchenlong.chunyu.me.android_pedometer_set:cy_pedometer_set
進(jìn)程ID是
28490和28515. 父進(jìn)程ID是410.ps -help顯示標(biāo)題.
使用":"表示私有進(jìn)程, 其他組件不能使用; 使用全稱表示全局進(jìn)程, 其他組件可以使用ShareUID共享進(jìn)程.
多進(jìn)程無法通過內(nèi)存共享數(shù)據(jù). 可以通過Intent傳遞數(shù)據(jù).
不同進(jìn)程的組件會擁有獨(dú)立的虛擬機(jī), Application, 內(nèi)存空間.
多個進(jìn)程, Application會創(chuàng)建多次.
Serializable和Parcelable接口處理對象序列化過程. 使用ObjectOutputStream和ObjectInputStream處理對象的Serializable序列化與反序列化.
Intent可以使用
Serializable和Parcelable接口傳遞復(fù)雜對象數(shù)據(jù), 參與進(jìn)程間的通信.
OK, that's all! Enjoy it!