简体   繁体   English

远程服务拒绝对绑定的权限

[英]Remote Service deny permission onBind

I have a remote service, which external applications can bind to. 我有一个远程服务,外部应用程序可以绑定到该服务。 There are situations where I may wish to decline the binding. 在某些情况下,我可能希望拒绝绑定。 According to the documentation , 根据文件

Return the communication channel to the service. 将通信通道返回给服务。 May return null if clients can not bind to the service. 如果客户端无法绑定到服务,则可能返回null。

@Override
public IBinder onBind(final Intent intent) {
    return null;
}

Returning null does indeed not return an IBinder object and therefore prevents the connection, however the calling application does not correctly receive this 'information'. 返回null确实不会返回IBinder对象,因此会阻止连接,但是调用应用程序无法正确接收此“信息”。

boolean bound = context.bindService(intent, serviceConnection, flagsHere);

Whether returning null or not from the Service, this always returns true? 无论是否从服务返回null,这总是返回true?

According to the documentation , 根据文件

Returns - If you have successfully bound to the service, true is returned; 返回 - 如果已成功绑定到服务,则返回true; false is returned if the connection is not made so you will not receive the service object 如果未建立连接,则返回false,因此您将不会收到服务对象

I had assumed that returning null from onBind would have caused bindService to return false. 我曾假设从onBind返回null会导致bindService返回false。 Assumptions are never a good idea... 假设永远不是一个好主意......

Returning null does however prevent the ServiceConnection from being instantiated invoked, but a consequence of this would be no option to check if the binder is in fact null in onServiceConnected . 但是,返回null确实会阻止ServiceConnection 实例化 调用,但是这样做的结果就是没有选项来检查绑定在onServiceConnected中是否实际为null。

So, my question - How does an application 'know' if the binding request has been denied? 所以,我的问题 - 如果绑定请求被拒绝,应用程序如何“知道”?

Additionally, if I decide on the fly that a request to onRebind (having previously returned true from onUnbind ) should be declined, I seem to be unable to override the behaviour to prevent this: 另外,如果我在飞行中决定对onRebind (先前从onUnbind返回true)的请求应该被拒绝,我似乎无法覆盖行为以防止这种情况:

@Override
public void onRebind(final Intent intent) {

    if (shouldAllowRebind(intent)) {
        super.onRebind(intent);
    } else {
        // ?
    }
}

I hope someone can shed some light for me. 我希望有人可以为我揭开光明。 Thanks in advance. 提前致谢。

You probably have to create a workaround. 您可能需要创建一个变通方法。 I see two options here: 我在这里看到两个选项:

  • Return a Binder without any functionality if the request should be denied. 如果请求被拒绝,则返回没有任何功能的Binder The client then has to check if the wanted functionality is there. 然后客户端必须检查是否存在所需的功能。
  • Always return the same Binder , but let every method throw an Exception (eg SecurityException ) if the call is not permitted. 总是返回相同的Binder ,但如果不允许调用,则让每个方法抛出一个Exception (例如SecurityException )。 (This was also suggested by @CommonsWare in the comments) (这也是@CommonsWare在评论中提出的)

I would personnaly prefer the second approach as it is more flexible. 我个人更喜欢第二种方法,因为它更灵活。 (eg allows per-call permit/deny, solves the problem of denying stuff after a rebind, etc.) (例如,允许每次呼叫许可/拒绝,解决重新绑定后拒绝内容的问题等)

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

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