Android: onConnected、onConnectionSupspended 或 onConnectionFailed 都没有被调用

Android: neither onConnected, onConnectionSupspended or onConnectionFailedare being called

我正在制作一个天气应用程序,除了获取用户的位置外,一切正常。我正在尝试使用播放服务 API 并调用 getLongitude() 和 getLatitude() 来做到这一点。但是,我正在编写的代码中 none 是有效的,因为我在标题中列出的代码中 none 是 运行。这是与获取位置有关的代码:

public class MainActivity extends ActionBarActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
public static final String TAG = MainActivity.class.getSimpleName();
private CurrentWeather mCurrentWeather;
private GoogleApiClient mGoogleApiClient;
private double mLongitude;
private double mLatitude;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "Location services connected.");
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    mLatitude = location.getLatitude();
    mLongitude = location.getLongitude();
}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "Location services suspended. Please reconnect.");

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "Location services failed.");

}

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

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

您错过了初始化 GoogleApiClient 对象..

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

 mGoogleApiClient.connect();

请添加以上代码并检查。

在 gradle 中使用此库。 com.google.android.gms:播放服务:8.4.0

getLocation 参考下面link。

Fused location provider example

onConnected() 中返回的位置可能为 NULL。所以你需要检查里面的 onConnected()

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

if (location != null) {
    mLatitude = location.getLatitude();
    mLongitude = location.getLongitude();
}

现在它不会再次获得位置更新,因此您需要按如下方式请求位置更新 -

创建位置请求对象

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FATEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

您可以在 onCreate() 本身调用上述方法。

现在添加下面的代码行以在 onConnected() 方法中获取位置更新 -

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

通过以下代码获取用户位置后,您可以停止位置更新。

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);