我按照在线文章尝试使用 AIDL 创建自己的 IPC 示例。以下是我的代码片段
ipc_library
// IIPCInterface.aidl
package com.zzzz.ipc_library;
// Declare any non-default types here with import statements
interface IIPCInterface
{
int returnValues();
}
ipc_server
package com.zzzz.ipc_server
import com.zzzz.ipc_library.IIPCInterface
class IPCServiceImpl : IIPCInterface.Stub()
{
override fun returnValues(): Int
{
val numberValues = 1
return numberValues
}
}
package com.zzzz.ipc_server
import android.app.Service
import android.content.Intent
import android.os.IBinder
class IPCService : Service()
{
override fun onBind(p0: Intent): IBinder?
{
return IPCServiceImpl()
}
}
ipc_client
class MainActivity : AppCompatActivity(), ServiceConnection
{
override fun onStart() {
super.onStart()
Log.v("AIDL", "onStart calling bindService")
bindService(Intent("com.zzzz.ipc_server.IPCService").setPackage("com.zzzz.ipc_server"),this,BIND_AUTO_CREATE)
}
override fun onCreate(savedInstanceState: Bundle?) {
....
Log.v("AIDL", "returnValues " + ipcservice?.returnValues())
....
}
private var ipcservice: IIPCInterface? = null
override fun onServiceConnected(p0: ComponentName?, p1: IBinder?){
Log.v("AIDL", "onServiceConnected called")
Log.v("AIDL", "")
ipcservice = IIPCInterface.Stub.asInterface(p1)
}
override fun onServiceDisconnected(p0: ComponentName?) {
TODO("Not yet implemented")
}
}
我在onStart中绑定服务
我从来没有看到登录 onServiceConnected 被调用,所以似乎函数没有被执行。
returnValues 的日志记录显示 null 而不是 1.
我主要按照本文中的步骤进行操作(https://budhdisharma.medium.com/aidl-and-its-uses-in-android-e7a2520093e)。
有没有我漏掉的步骤?