轻量级多进程框架
maven {
url 'https://jitpack.io'
}
implementation 'com.github.lavalike:service-manager:1.0.0'
在Application中初始化:
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
// 每个进程都要初始化
ServiceManager.init(this)
}
}
分别定义服务接口与实现类
interface Api {
fun call(): String
}
class ApiImpl : Api {
override fun call(): String = "Hello Api, Process: ${android.os.Process.myPid()}"
}
在任意进程中发布服务
ServiceManager.publishService(SERVICE_NAME, ApiImpl::class.java.name)
在任意进程中获取服务并调用
ServiceManager.getService(SERVICE_NAME)?.let {
Toast.makeText(this, (it as Api).call(), Toast.LENGTH_SHORT).show()
}