Android我正在开发的应用程序有两个服务ServiceA- 和ServiceB,前者启动后者。ServiceB使用GoogleAPIClient接收和提供位置作为意图ServiceA和其他客户端。
我正在尝试使用 GoogleAPIClient 来获取位置,ServiceB如下所示:
@Override
public void onCreate() {
mGoogleApiClient = new GoogleApiClient.Builder(ServiceB.this)
.addConnectionCallbacks(mConnectionCallbacks)
.addOnConnectionFailedListener(mOnConnectionFailedListener)
.addApi(LocationServices.API).build();
mGoogleApiClient.connect();
}
ServiceA关于如何开始稍后开始有两种情况ServiceB:
由 Activity绑定和启动。
Intent serviceA = new Intent(context, ServiceA.class); bindService(serviceA, mConnection, Context.BIND_AUTO_CREATE);在启动时,通过一个
BroadcastReceiver监听 android.intent.action.BOOT_COMPLETED 。@Override public void onReceive(Context context, Intent intent) { Intent serviceA = new Intent(context, ServiceA.class); startWakefulService(context, serviceA); }
启动后,ServiceA开始ServiceB是这样的:
Intent startIntent = new Intent(ServiceA.this, ServiceB.class);
startService(startIntent);
在这两种情况下,ServiceA 和 ServiceB 都会启动。
情况 1(从 开始Activity)一切正常,但是当使用情况 2(启动时开始)时,在 ServiceB 启动并调用之后,即使等待几分钟mGoogleApiClient.connect(),也没有通知mOnConnectionFailedListenerand 。mConnectionCallbacks
我不明白GoogleAPIClient最终无法正常工作的两种情况之间的区别是什么,但我怀疑它与用于 start 的 Context 有关ServiceA,因为它是Activityon case 1 和on case 2 上Context提供的BroadcastReceiver。
我需要帮助来解决这个问题。
提前致谢。
更新 1
我正在使用 Google Play 服务 8.4.0 版
compile 'com.google.android.gms:play-services:8.4.0'
更新 2
更改为 9.2.1 版本后仍然没有回调。