java - 为什么我的应用程序抛出 IllegalStateException GoogleApiClient 尚未连接?

标签 java android google-api google-api-client

java.lang.IllegalStateException: GoogleApiClient is not connected yet.

这来 self 的服务 (GetInformationService.onConnected)。奇怪的是它在我的 onConnected 方法中说它没有连接?我见过很多这样的问题,但我仍然无法弄清楚,因为所有答案都说了有关 onCreate 方法的内容,但我没有 onCreate,因为这是一项服务。这是我的相关代码(请记住我正在扩展 IntentService):

@Override
protected void onHandleIntent(Intent intent)
{
    g = Globals.getInstance();
    context = this;


    locationProvider = LocationServices.FusedLocationApi;
    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    locationRequest = new LocationRequest();
    locationRequest.setInterval(60000);
    locationRequest.setFastestInterval(15000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    googleApiClient.connect();
}


@Override
public void onConnected(Bundle bundle) 
{
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

最佳答案

尝试在您的服务中包含 onCreate() 并从此处构建 googApiClient

    @Override
public void onCreate() {
    super.onCreate();
    googleApiClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
    googleApiClient.connect();
      }

关于java - 为什么我的应用程序抛出 IllegalStateException GoogleApiClient 尚未连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35209024/

相关文章:

javascript - 仅从 Google API 加载特定字体

node.js - 将 google-api-javascript-client 与 node.js 结合使用

java - Google OAuth2 API v2 用户信息对于电子邮件始终返回 null

java - 获取从 llistview 中选择的项目,其中包含来自 Android Activity 的多个数据

java - 使用 jaxb 注释映射 XML 子子项

Android 服务不会从线程启动

android - 如何在 Android Studio 中访问 Android 引用

java - 如何使用正则表达式给出查找头的范围,例如 ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$

java - 在 Rhino 中访问 java map 和列表作为 JavaScript 对象

java - Action 图像捕捉导致观看时图像模糊