Android请求到另一个服务 - 显式vs隐式意图

时间:2016-05-22 16:04:08

标签: android android-intent

我正在尝试向另一个服务(另一个应用程序)发出请求,但获得异常:

java.lang.IllegalArgumentException: 
Service Intent must be explicit: Intent { act=com.myApp.DoAction }

我做了一些阅读但仍然困惑:
Here,它写了:Since Android 5.0 (Lollipop) bindService() must always be called with an explicit intent.
Here,已写完:Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it

什么是正确的意图?什么是正确的例子? 我在做:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setAction("com.anotherApp.MyService");
// binding to remote service
bindService(intent, AddServiceConnection, Service.BIND_AUTO_CREATE);

许多人都感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我相信这一点 Intent intent = new Intent(Intent.ACTION_SEND);

代码行说意图行为是隐含的。它通过抽象SEND操作声明了意图。尝试删除Intent.ACTION_SEND

答案 1 :(得分:0)

尝试一些教程后: 意图是隐含的this nice example shows how to deal with it

Intent intent = new Intent("com.anotherapp.MyService");
intent.setPackage("com.anotherapp");
bindService(intent, AddServiceConnection, Service.BIND_AUTO_CREATE);