关于 Android 进程的简介

在Android系统中, 进程非常重要, 除了主进程运行App, 我们还可以使用其他进程处理独立任务.

更多: http://www.wangchenlong.org/
欢迎Follow我的GitHub: https://github.com/SpikeKing

进程, 即Process. 进程间通信, 即IPC(Inter-Process Communication).

Process

在Android中, 使用多进程只有一种方式, 在AndroidManifest中, 为四大组件(Activity, Service, Receiver, ContentProvider)指定android:process属性.

1
2
3
4
<service
android:name=".PedometerCounterService"
android:exported="false"
android:process=":cy_pedometer_set"/>

exported="false"表示只与本应用内的进程通信, 即包名相同.

默认进程的进程名是包名.

1
2
3
➜  ~ 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是2849028515. 父进程ID是410. ps -help显示标题.

使用":"表示私有进程, 其他组件不能使用; 使用全称表示全局进程, 其他组件可以使用ShareUID共享进程.

多进程无法通过内存共享数据. 可以通过Intent传递数据.

不同进程的组件会拥有独立的虚拟机, Application, 内存空间.

多个进程, Application会创建多次.

SerializableParcelable接口处理对象序列化过程. 使用ObjectOutputStreamObjectInputStream处理对象的Serializable序列化与反序列化.

Intent可以使用SerializableParcelable接口传递复杂对象数据, 参与进程间的通信.

OK, that’s all! Enjoy it!


最初发布地址:
http://www.wangchenlong.org/2016/05/06/1605/061-android-process-concept/
欢迎Follow我的GitHub, 关注我的简书, CSDN, 掘金.
我已委托“维权骑士”为我的文章进行维权行动. 未经授权, 禁止转载, 授权或合作请留言.