java - 无法连接到 Google API 客户端?

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

我已经在这个问题上坚持了一个多星期了,我似乎无法弄清楚我到底做错了什么。我已阅读以下问题,但这些问题似乎都不适合我:

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

GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called

google api client callback is never called

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

我想做的是结合使用 LocationServices API 和 Geofence 类来检查用户是否位于指定区域。问题似乎在于与 Google API 客户端和/或 Locationservices API 的通信。

我已在 list 中声明以下内容:

<service android:name=".GeofenceTransitionsIntentService" />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="value of my key" />

</application>

我在项目中声明了 2 个与 GoogleApiClient 交互的 Java 方法:

 protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

public void startApiClient(){

    if (mGoogleApiClient.isConnected()) {
        Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
        return;

        //Of course the Toast is supposed to fire of when the user cannot
        //connect to the google api. However when I make this code to run
        //when the user cannot connect to google play services it just shows 
        //the toast and does not report me the error which is why I in this 
        //case made this code to run when the user is connected to google play services

    }

    try {
        LocationServices.GeofencingApi.addGeofences
                (mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent()).setResultCallback(this); // Result processed in onResult(). This is also the line where the app seems to crash because it is unable to connect to the google api client
    } catch (SecurityException securityException) {
        securityException.notify();
    }
}

然后,我在 onCreate() 方法中调用 buildGoogleApiClient() ,并在 onStart() 方法中调用 startApiClient() :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);

…

buildGoogleApiClient();
}

_

@Override
protected void onStart() {
    super.onStart();

    mGoogleApiClient.connect();

    startApiClient();

}

如果有必要,onResult() 方法:

public void onResult(Status status) {
    if (status.isSuccess()) {
        // Update state and save in shared preferences.
        mGeofencesAdded = !mGeofencesAdded;
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(Constants.GEOFENCES_ADDED_KEY, mGeofencesAdded);
        editor.apply();
    }

奇怪的是,连接实际上会实例化一段时间。当我告诉程序在用户未连接时显示 Toast 时,我的 Android 监视器告诉我已建立连接,但 Toast 仍然会被触发。但是,当我告诉程序在用户连接到 Google API 客户端时显示 Toast 时,应用程序崩溃并告诉我 Google API 客户端尚未连接。

预先感谢您抽出宝贵时间来帮助我解决此问题。如果我正在监督一些非常明显的事情,请原谅我。

最佳答案

请尝试阅读 AndroidHive 中的 Google 客户端位置教程 - Android Location API using Google Play Services .

如给定教程中所述,您需要在 Activity 中进行以下更改才能获取用户的当前位置。

  1. First check for availability of Google Play Services by calling checkPlayServices() in onResume()

  2. Once play services are available on the device, build the GoogleApiClient by calling buildGoogleApiClient() method.

  3. Connect to google api client by calling mGoogleApiClient.connect() in onStart() method. By calling this, onConnectionFailed(), onConnected() and onConnectionSuspended() will be triggered depending upon the connection status.

  4. Once google api is successfully connected, displayLocation() should be called in onConnected() method to get the current location.

此外,您还可以检查以下可能导致警报未按 Troubleshoot the Geofence Entrance Event 中给出的预期工作的原因。 。这些是:

  • 您的地理围栏内无法提供准确的位置,或者您的地理围栏太小。
  • 设备上的 Wi-Fi 已关闭。确保您的设备已启用 Wi-Fi 和位置功能。<​​/li>
  • 您的地理围栏内没有可靠的网络连接。
  • 提醒可能会延迟。

重要信息和示例代码可以在Creating and Monitoring Geofences中找到并在给定的教程中。

关于java - 无法连接到 Google API 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38730113/

相关文章:

java - Okhttp 或 HTTPClient : Which offers better functionality and more efficiency?

java - java中使用itext将jsp转为pdf

Android WebView 超时

android - Azure BLOB 存储 REST API - 使用 ADAL 访问 token 返回 403 和 404 错误

android - 从服务 : alternate flow of events 使用 Google Play 服务

android - Google Play 游戏登录流程

google-play - 已发布游戏上的 "The Application is incorrectly configured"

Java:使用 onComplete 回调简单实现命令模式?

java - 如何测试我的 Java 应用程序是否可以成功处理 Tomcat 服务器上的低内存/CPU 资源?

java - Android-卡在正在运行的项目上