Android中的多进程

在Android中也可以像pc一样开启多进程,这在android的编程中通常是比较少见的,以为在一个app基本上都是单进程工作就已经足够了,有一些特殊的场景,我们需要用多进程来做一些额外的工作,比如下载工作等。

在Android的AndroidManifest.xml 中,每一个activity或者service 都可以指定一个进程名称android:process,当这个activity或者service 被调用时,该进程自动启动。

因此在android中启动一个进程是比较简单的,如果需要看一个app有几个进程,看AndroidManifest.xmlandroid:process 就能知道有几个进程。

建立一个其他进程的service

这里用一个service建立其他的一个进程。RemoteService 是一个空的service。代码如下:

kotlin 复制代码
class RemoteService : Service() {

    private var TAG = "RemoteService"

    override fun onBind(intent: Intent?): IBinder? = null

    override fun onCreate() {
        //Debug.waitForDebugger();
        super.onCreate()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }
}

AndroidManifest.xmlandroid:process 的值取为:

android:process=":remote"

这时候,只需要在代码中启动这个服务,进程自然就建立了。

kotlin 复制代码
val serviceIntent = Intent(this, RemoteService::class.java)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(serviceIntent)
            } else {
                startService(serviceIntent)
            }

可以看到 com.first66.multi_process:remote的进程已经启动了。

进程间的通讯AIDL

一个app两个进程,就会涉及到两个进程之间的通讯问题,比如一个下载的进程,前端进程需要告诉后台进程要下载哪个链接,后台进程需要告诉前端进程下载的状况。

在android中进程间的通讯可以使用AIDL进行,相当于对服务进行对象的bind

创建 IMessageInterface.aidl 的aidl,用来进行两进程间的通讯。

kotlin 复制代码
interface IMessageInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void loadData(in String message);
}

这个时候IMessageInterface 只是一个接口而已,相当于一个Binder需要进行实例化。

RemoteService.kt 中创建一个Binder,当每个对象要来绑定这个服务的时候,我们返回这个Binder给他。

kotlin 复制代码
    private var binder:Binder = object : IMessageInterface.Stub() {
        override fun loadData(message: String?) {
            message?.let {
                Log.d(TAG,it)
            }
        }
    }

绑定的时候,把这个Binder 给另一个进程。

kotlin 复制代码
    override fun onBind(intent: Intent?): IBinder? {
        return binder
    }

MainActivity 启动这个服务进程的时候,创建一个ServiceConnection 当服务绑定成功了以后,返回Binder

kotlin 复制代码
private var iMessageAidlInterface: IMessageInterface? = null

    private var serviceConnection: ServiceConnection = object  : ServiceConnection{

        override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
            iMessageAidlInterface = IMessageInterface.Stub.asInterface(p1)
            iMessageAidlInterface?.loadData("Hello Message")
        }
        override fun onServiceDisconnected(p0: ComponentName?) {
        }
    }

onServiceConnected 连接成功了以后会IBinder 返回给启动的进程,这个就能给另外一个进程传递消息了。

进程间的调试

在调试的时候,下的断点必须是在同一个进程间才能够停的住,如果是处在不同的进程,即使下了断点也是会变黑的。

可以在另外一个进程服务中onCreate 加入:

kotlin 复制代码
Debug.waitForDebugger();

当启动服务的时候,点击另外一个进程,断点才能起作用。

查看进程:

点击 com.first66.multi_process:remote 进入调试。

相关推荐
andyweike3 小时前
Android Automotive
android
又见情义3 小时前
记一次 RK3568 Android 13 蓝牙反复重启问题的排查与修复
android
hai_android3 小时前
Kotlin Flow combine 源码剖析:一个 Channel 如何优雅合并多条流
android·java·kotlin
敲代码的瓦龙5 小时前
Jetpack?ViewModel!!!
android·开发语言·kotlin·android-studio
Godikov5 小时前
Android 工控终端实战:从秒级卡顿到毫秒响应,SQLite/LitePal 性能优化全记录
android
星间都市山脉7 小时前
Android16 SystemService.onBootPhase 调用时机
android·java·linux·windows·ubuntu
swithun7 小时前
不用 WebView,我用 Kotlin + Compose Multiplatform 重写 Mermaid,并做了 2048 组对拍
android·开源·kotlin
AI吃大瓜8 小时前
人脸检测和行人检测4:Android实现YOLOv8 YOLO11 YOLO26人脸检测和人体检测(含源码,可实时检测)
android·yolo·人脸检测·人体检测·行人检测·yolo26
与海boy8 小时前
Prompt模版
android·prompt
FlightYe8 小时前
音视频修炼之编码器(一):AVC、HEVC编码器内部
android·linux·c++·音视频