本文的合集已經(jīng)編著成書,高級Android開發(fā)強化實戰(zhàn)吆视,歡迎各位讀友的建議和指導典挑。在京東即可購買:https://item.jd.com/12385680.html
在Android系統(tǒng)中, 進程非常重要, 除了主進程運行App, 我們還可以使用其他進程處理獨立任務.進程, 即Process. 進程間通信, 即IPC(Inter-Process Communication).
在Android中, 使用多進程只有一種方式, 在AndroidManifest中, 為四大組件(Activity, Service, Receiver, ContentProvider)指定android:process
屬性.
<service
android:name=".PedometerCounterService"
android:exported="false"
android:process=":cy_pedometer_set"/>
exported="false"
表示只與本應用內的進程通信, 即包名相同.
默認進程的進程名是包名.
? ~ 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
進程ID是
28490
和28515
. 父進程ID是410
.ps -help
顯示標題.
使用":"
表示私有進程, 其他組件不能使用; 使用全稱表示全局進程, 其他組件可以使用ShareUID
共享進程.
多進程無法通過內存共享數(shù)據(jù). 可以通過Intent傳遞數(shù)據(jù).
不同進程的組件會擁有獨立的虛擬機, Application, 內存空間.
多個進程, Application會創(chuàng)建多次.
Serializable
和Parcelable
接口處理對象序列化過程. 使用ObjectOutputStream
和ObjectInputStream
處理對象的Serializable
序列化與反序列化.
Intent可以使用
Serializable
和Parcelable
接口傳遞復雜對象數(shù)據(jù), 參與進程間的通信.
OK, that's all! Enjoy it!