Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用AIDL绑定服务与使用操作绑定?_Android_Service_Ipc_Aidl - Fatal编程技术网

Android 使用AIDL绑定服务与使用操作绑定?

Android 使用AIDL绑定服务与使用操作绑定?,android,service,ipc,aidl,Android,Service,Ipc,Aidl,根据开发者论坛,以下是我如何使用AIDL绑定我的服务: /* Establish a couple connections with the service, binding by interface names.This allows other applications to be installed that replace the remote service by implementingthe same interface.*/ bindService(new Intent(

根据开发者论坛,以下是我如何使用AIDL绑定我的服务:

/* Establish a couple connections with the service, binding by interface names.This allows  other  applications to be installed that replace the remote service by implementingthe same  interface.*/

bindService(new Intent(IRemoteService.class.getName()),mConnection, Context.BIND_AUTO_CREATE);
现在我有了应用程序A,它有服务类和AIDL接口

我有应用程序B,我希望通过它访问应用程序A的服务类,使用应用程序A中公开的ny AIDL方法

所以我在应用程序B中为AIDL创建了完全相同的包,并从应用程序A粘贴了AIDL

但当我尝试使用文档中提到的上述机制绑定服务时,它告诉我:

无法启动服务意图{act com.example.service.MyService }U=0:找不到

应用程序内服务

导出=真

现在,我不再使用这种方法,而是在应用程序A的服务中添加了意图过滤器,并尝试使用

intent.setClassName(String packageName, String className)
现在它开始工作了


那么,我在参考原始文档时是否遗漏了什么?什么时候会使用这种方法?

在与pskink讨论上述评论后,我得到了这个问题的答案。答案在于我的新实现

检查文档中上述示例中使用的构造函数。它告诉我们,在意图构造函数中传递的IRemoteService.class.getName只不过是一个“操作”。因此,如果我希望我的代码能够找到我希望绑定的服务,那么由IRemoteService.class.getName返回的字符串应该作为我的服务的意图过滤器中的一个操作提到

这里要考虑的两点是:

这不会遵循操作的标准命名约定

使用隐式意图启动服务会引起警告,因为使用startService的隐式意图是不安全的,原因很明显,如果多个服务使用相同的操作,那么这将是一个问题


IRemoteService.class.getName返回什么?您是如何在清单中定义您的服务的?请参考我问题开头的链接,该链接涉及Android documenataion。你没有回答,你是如何在舱单上声明你的服务的?因此,添加一个具有唯一操作e的意图过滤器。Gcom.example.service.MyService.ACTION\u启动并将该字符串传递给Intent构造函数