无法连接到Google API CLient?

时间:2016-08-02 20:44:16

标签: 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进行通信。

我在清单中声明了以下内容:

<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>

我在项目中声明了两个与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客户端尚未连接。

提前感谢您抽出时间帮助我解决这个问题。如果我监督一些非常明显的事情,请原谅我。

1 个答案:

答案 0 :(得分:0)

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

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

  
      
  1. 首先通过checkPlayServices()

  2. 中的onResume()来查看Google Play服务的可用性   
  3. 在设备上提供播放服务后,请通过调用buildGoogleApiClient()方法构建GoogleApiClient。

  4.   
  5. 通过mGoogleApiClient.connect()方法调用onStart()连接到google api客户端。通过调用此方式,系统会根据连接状态触发onConnectionFailed()onConnected()onConnectionSuspended()

  6.   
  7. 成功连接google api后,应displayLocation()方法调用onConnected()以获取当前位置。

  8.   

此外,您还可以检查以下可能导致警报无法正常工作的原因,如Troubleshoot the Geofence Entrance Event中所述。这些是:

  • 地理围栏内没有准确的位置,或者地理围栏太小。
  • 设备上的Wi-Fi已关闭。确保您的设备已启用wifi和位置。
  • 您的地理围栏内没有可靠的网络连接。
  • 提醒可能会迟到。

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