GoogleApiClient尚未连接,即使已调用onConnected并且我在onCreate中创建了GoogleApiClient

时间:2015-11-14 07:40:12

标签: java android location google-api-client

我在这里看了这个Q& A:GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called

因为它似乎与我所经历的相似,但事实并非如此。该用户的问题是他们在onStart()方法中声明了他们的api客户端,我在onCreate()方法中创建了我的答案。然而,我仍然遇到同样的错误。

以下是我对这三种方法的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* Butter knife creates the variables */
    ButterKnife.bind(this);

    /* Startup location services */
    locationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    progressBar.setVisibility(View.INVISIBLE);

    refreshImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getForecast();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
    Log.i("Connected!!!", "WERE CONNECTED");
}

@Override
protected void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }

    resumeLocationUpdates();

}
private void resumeLocationUpdates() {
    Log.i("RESUMING", "RESUMING LOCATION UPDATES");
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}

这是记录器显示的内容,这让我感到困惑,因为它显示已连接但应用程序崩溃说没有连接......

-14 02:25:13.582 25885-25885/? I/Connected!!!: WERE CONNECTED
11-14 02:25:13.582 25885-25885/? I/RESUMING: RESUMING LOCATION UPDATES
11-14 02:25:13.582 25885-25885/? D/AndroidRuntime: Shutting down VM
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: FATAL EXCEPTION: main
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: Process: lpadron.me.weatherly, PID: 25885
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {lpadron.me.weatherly/lpadron.me.weatherly.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.

2 个答案:

答案 0 :(得分:5)

您的问题出在onResume逻辑:

@Override
protected void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }

    resumeLocationUpdates();

}
private void resumeLocationUpdates() {
    Log.i("RESUMING", "RESUMING LOCATION UPDATES");
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}

mGoogleApiClient.connect()的调用是异步的。它在连接完成之前返回,并且您在连接客户端之前请求位置更新。您需要将requestLocationUpdates调用移至GoogleApiClient.onConnected回调。在此活动之后,您的客户端已连接。

@Override
protected void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }   
}

@Override
public void onConnected(Bundle bundle) {
    resumeLocationUpdates();
}

答案 1 :(得分:1)

  1. 您的课程必须实施GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener并覆盖所有方法。

  2. GoogleApiClient将与LocationServices通信,并为用户提供经度和纬度。

  3. 在您的OnCreate()方法中构建一个使用所需api的GoogleApiClient。

  4. 在onStart()方法中,开始连接GoogleApiClient。

  5. 在Sucessfull连接上调用OnConnected()方法,我们将在这里进行LocationRequest。

  6. 一旦我们发出请求onLocationChanged()方法,我们将使用location对象连续获取纬度和经度更新。
  7. 当连接挂起且连接失败时,将使用
  8. onConnectionSuspended(),onConnectionFailed()。
  9. 别忘了在onStop()方法中断开Goog​​leApiClient连接,否则会浪费大量资源。
  10. 在这里观看教程 Android Location API Using Google Play Services