gpt4 book ai didi

android - 如何从不同进程中的远程服务向 IPC 客户端发送数据

转载 作者:行者123 更新时间:2023-12-04 17:22:54 24 4
gpt4 key购买 nike

有一个IPC客户端和一个IPC服务器(远程服务)属于不同的进程。我要开始发送数据连续绑定(bind)到远程服务后发送给客户端。我可以绑定(bind)到服务并从客户端调用 AIDL 方法,但是我找不到方法 从远程服务发送数据 给客户。

最佳答案

这可以通过在 AIDL 中使用远程回调来实现。 Server 实现了一个 AIDL,它允许客户端在绑定(bind)到服务后注册一个远程回调(回调也是一个 AIDL)。然后服务器可以在需要时调用远程回调向客户端发送数据。请参阅以下来自 Android API 演示的代码 fragment 。
IRemoteService.aidl

interface IRemoteService {
/**
* Often you want to allow a service to call back to its clients.
* This shows how to do so, by registering a callback interface with
* the service.
*/
void registerCallback(IRemoteServiceCallback cb);

/**
* Remove a previously registered callback interface.
*/
void unregisterCallback(IRemoteServiceCallback cb);
}
IRemoteServiceCallback.aidl
oneway interface IRemoteServiceCallback {
/**
* Called when the service has a new value for you.
*/
void valueChanged(int value);
}
客户端和服务器端代码都可以在 RemoteService.java 看到
客户端代码注册远程回调
        private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. We are communicating with our
// service through an IDL interface, so get a client-side
// representation of that from the raw service object.
mService = IRemoteService.Stub.asInterface(service);
mKillButton.setEnabled(true);
mCallbackText.setText("Attached.");

// We want to monitor the service for as long as we are
// connected to it.
try {
mService.registerCallback(mCallback);
} catch (RemoteException e) {
// In this case the service has crashed before we could even
// do anything with it; we can count on soon being
// disconnected (and then reconnected if it can be restarted)
// so there is no need to do anything here.
}
调用客户端远程回调的服务器端代码
               `// Broadcast to all clients the new value.
final int N = mCallbacks.beginBroadcast();
for (int i=0; i<N; i++) {
try {
mCallbacks.getBroadcastItem(i).valueChanged(value);
} catch (RemoteException e) {
// The RemoteCallbackList will take care of removing
// the dead object for us.
}
}
mCallbacks.finishBroadcast();`

关于android - 如何从不同进程中的远程服务向 IPC 客户端发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65127246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com