无法连接到Google API CLient? [英] Unable to connect to Google API CLient?

查看:506
本文介绍了无法连接到Google API CLient?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

致命异常:java.lang.IllegalStateException GoogleApiClient尚未连接



< GoogleApiClient正在抛出GoogleApiClient尚未连接的提示,而GoogleApiClient正在抛出GoogleApiClient尚未连接。在调用onConnected函数后



google api client callback永远不会被调用

导致:java.lang.IllegalStateException:GoogleApiClient尚未连接



我想要做的是将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 =my key的值/>

< / application>

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

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

$ b $ public void startApiClient(){

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

//当然,当用户不能
//连接到google api时,Toast应该会触发。然而,当我让这段代码运行
//当用户无法连接到谷歌播放服务时,它只是显示
//敬酒,并且不向我报告错误,这就是为什么我在这
//这个代码在用户连接到google play服务时运行

}

try {
LocationServices.GeofencingApi.addGeofences
(mGoogleApiClient,getGeofencingRequest(),getGeofencePendingIntent())。setResultCallback(this); //在onResult()中处理结果。这也是应用程序似乎崩溃的行,因为它无法连接到谷歌api客户端
} catch(SecurityException securityException){
securityException.notify();


$ / code>

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

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

...

buildGoogleApiClient();

$ / code>

_

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

mGoogleApiClient.connect();

startApiClient();在需要的情况下,onResult()方法:




$ b

/ p>

  public void onResult(Status status){
if(status.isSuccess()){
//更新状态并保存在共享首选项中。
mGeofencesAdded =!mGeofencesAdded;
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(Constants.GEOFENCES_ADDED_KEY,mGeofencesAdded);
editor.apply();

$ / code>

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



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

解决方案

请尝试阅读AndroidHive中的Google Client位置教程 - 使用Google Play服务的Android位置API



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



  1. 首先通过调用 checkPlayServices()来检查Google Play服务的可用性。 code> onResume()


  2. 设备上提供播放服务后,通过调用<$ c来构建GoogleApiClient $ c> buildGoogleApiClient()方法。

  3. 通过调用 mGoogleApiClient.connect( )在 onStart()方法中。通过调用这个函数,onConnectionSailed() onConnectionFailed() c $ c>将根据连接状态被触发。 应该在 onConnected()方法中调用c $ c>来获取当前位置。


此外,您还可以检查以下可能的原因,使警报无法按预期方式工作,如排除Geofence入场事件。这些是:


  • 准确的位置在您的地理围栏内不可用或您的地理围栏太小。

  • 设备上的Wi-Fi已关闭。确保设备上的wifi和位置已启用。

  • 您的geofence中没有可靠的网络连接。
  • 警报可能会延迟。



重要信息和示例代码可以在创建并监控地理围栏和给定的教程。


I have been stuck on this for over a week now and I can't seem to figure out what exactly it is that I'm doing wrong. I have read the following questions, none of which seem to work for me:

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

What I'm trying to do is use the LocationServices API in combination with the Geofence class to check if the user is in a specified area. The problem seems to lie in communicating with the Google API Client and/or Locationservices API.

I have declared the following in my manifest:

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

I have 2 Java methods declared in my project which interact with the GoogleApiClient:

 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();
    }
}

I then call the buildGoogleApiClient() in my onCreate() method and call startApiClient() in my onStart() method:

@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();

}

And in case neccessary, the onResult() method:

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();
    }

The strange thing about this is that a connection actually does get instantiated for some time. When I tell the program to show the Toast when the user is not connected, my Android Monitor tells me that a connection has been made but the Toast still gets triggered. However when I tell the program to show the Toast when the user is connected to the Google API Client the app crashes and tells me that the Google API Client has not connected yet.

Thanks in advance for taking time out of your day to help me with this. Please forgive me if I'm overseeing something very obvious.

解决方案

Please try going through this tutorial of Google Client Location in AndroidHive - Android Location API using Google Play Services.

As discussed in the given tutorial, you need to do below changes in your activity to get the user’s current location.

  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.

Additionally, you can also check the following possible reasons for alerts not working as expected as given in Troubleshoot the Geofence Entrance Event. These are:

  • Accurate location is not available inside your geofence or your geofence is too small.
  • Wi-Fi is turned off on the device. Make sure that the wifi and location is enabled on your device.
  • There is no reliable network connectivity inside your geofence.
  • Alerts can be late.

Important information and sample codes can be found in Creating and Monitoring Geofences and in the given tutorial.

这篇关于无法连接到Google API CLient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆