简体   繁体   English

客户端进程(在带有 aidl 的 android IPC 中)如何知道远程服务器类?

[英]How a client process (in android IPC with aidl) be aware of remote sever class?

In Android official Aidl document, the IPC client example declares an intent explicitly with the target "RemoteService.class".在 Android 官方 Aidl 文档中,IPC 客户端示例明确声明了一个带有目标“RemoteService.class”的意图。 However, when server and client are not in the same package, client should not be aware of what is "RemoteService" if no dependency set.但是,当服务器和客户端不在同一个包中时,如果没有设置依赖项,客户端不应该知道什么是“RemoteService”。 How does the example works?这个例子是如何工作的?

ref: https://developer.android.com/guide/components/aidl.html参考: https ://developer.android.com/guide/components/aidl.html

I searched for several working examples, and the intent is set with Action instead of the remote service class object.我搜索了几个工作示例,意图是使用 Action 而不是远程服务类对象设置的。

In Android docs,在安卓文档中,

Intent intent = new Intent(Binding.this, RemoteService.class);
intent.setAction(IRemoteService.class.getName());
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

Currently, I expect this should be modified to:目前,我希望这应该修改为:

Intent intent = new Intent("<remote-service-intent-filter-in-androidmanifest>");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

You are on the right path, but if you add the intent action at the manifest, then you should also mention the package name while binding the service.您走在正确的道路上,但是如果您在清单中添加意图操作,那么您还应该在绑定服务时提及包名称。

intent.setPackage("<remote service package name>");

Caution: To ensure that your app is secure, always use an explicit intent when starting a Service and don't declare intent filters for your services.警告:为确保您的应用程序安全,请在启动服务时始终使用明确的意图,并且不要为您的服务声明意图过滤器。 Using an implicit intent to start a service is a security hazard because you cannot be certain of the service that responds to the intent, and the user cannot see which service starts.使用隐式意图启动服务存在安全隐患,因为您无法确定响应该意图的服务,并且用户无法看到启动了哪个服务。 Beginning with Android 5.0 (API level 21), the system throws an exception if you call bindService() with an implicit intent.从 Android 5.0(API 级别 21)开始,如果您使用隐式意图调用 bindService(),系统将抛出异常。 https://developer.android.com/guide/components/services https://developer.android.com/guide/components/services

Snipplet: Here is how I connect to a remote service on a different application with the setClassName API.片段:这是我如何使用 setClassName API 连接到不同应用程序上的远程服务。

Note : This approach does not need the intent action at the manifest file.注意:此方法不需要清单文件中的意图操作。

At Client activity.在客户活动中。

/**
 * Init Service
 */
private void initService() {
    if (mSampleService == null) {
        Intent i = new Intent();

        // set intent action
        i.setAction("com.hardian.sample.aidl.ISampleService");
        // mention package name with service's canaonical name
        i.setClassName("com.hardian.sample", "com.hardian.sample.aidl.SampleAidlService");

        // binding to a remote service
        bindService(i, mSampleServiceConnection, Service.BIND_AUTO_CREATE);
    } 
}

At Service在服务

 /**
 * {@inheritDoc}
 */
@Override
public IBinder onBind(Intent intent) {
    Log.d(TAG, "onBind called");
    if (ISampleService.class.getName().equals(intent.getAction())) {
        return mSampleServiceBinder;
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM