【发布时间】:2017-08-03 10:59:00
【问题描述】:
我有两个应该绑定到服务的应用程序。 应用 1 启动服务(如果尚未启动)。
startService(new Intent(this, Listener.class));
然后它绑定服务。
bindService(new Intent(this, Listener.class), mConnection, 0);
之后onServiceConnected 将被调用,Activity 将结束,服务将被解除绑定。服务仍在运行(bindService 中的“0”)。
直到这里一切都很好。
第二个 App 的代码看起来一模一样。但它不会启动服务,因为它已经运行。
bindService 返回真。所以一切看起来都很好。但是onServiceConnected 永远不会被调用。
我发现了这个:
onServiceConnected() not called
看起来像我的问题,但活动在同一个应用程序中......
我试过getApplicationContext.bindService,但在第一个应用程序中它抛出一个异常并且不绑定我的服务,在第二个它没有改变任何东西。
我想我需要更多类似getSystemContext 的东西,因为活动不在同一个应用程序中。
在我的 ManifestFiles 中,我输入了以下内容:
<service
android:name="com.example.tools.Listener"
android:label="Listener"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
<intent-filter>
<action android:name="com.example.tools.Listener" />
</intent-filter>
</service>
我希望有人可以帮助我。
最好的问候
法比安
【问题讨论】:
-
“第二个App”如何创建
Intent来启动“第一个”应用服务? -
你的“第二个应用程序”的代码是什么?你如何创建
Intent? -
@pskink “第二个App”的代码与“第一个App”的代码相同。所以 Intent 是这样创建的“new Intent(this, Listener.class)”。该服务位于两个应用程序都引用的库中。
-
如果您使用“应用二”中的
new Intent(this, Listener.class)那么您想如何在“应用一”中启动服务? -
@pskink 我在两个应用程序中使用相同的代码来绑定服务。 “第一个应用程序”在绑定服务之前启动服务。 “第二个检查服务是否已经启动,而不仅仅是绑定服务。如果没有
new Intent(this, Listener.class),我如何绑定它?
标签: android android-service android-service-binding onserviceconnected