Java Android:未调用OnConnect、onConnectionSupspended或OnConnectionFailed

Java Android:未调用OnConnect、onConnectionSupspended或OnConnectionFailed,java,android,google-play-services,Java,Android,Google Play Services,我正在制作一个天气应用程序,除了获取用户的位置,其他一切都正常。我正在尝试使用play services API并调用getLongitude()和getLatitude()。但是,我正在编写的代码都没有工作,因为我在标题中列出的代码都没有运行。以下是与获取位置有关的代码: public class MainActivity extends ActionBarActivity implements GoogleApiClient.OnConnectionFailedListener, Googl

我正在制作一个天气应用程序,除了获取用户的位置,其他一切都正常。我正在尝试使用play services API并调用getLongitude()和getLatitude()。但是,我正在编写的代码都没有工作,因为我在标题中列出的代码都没有运行。以下是与获取位置有关的代码:

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

您错过了初始化
GoogleAppClient
对象

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

 mGoogleApiClient.connect();
请添加以上代码并检查

使用gradle中的这个库com.google.android.gms:play services:8.4.0

有关getLocation,请参阅下面的链接。


在onConnected()中返回的位置可能为空。因此,您需要检查onConnected()中的

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

创建位置请求对象

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FATEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
您可以在onCreate()本身中调用上述方法

现在,在下面的代码行中添加OnConnect()方法中的位置更新-

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

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

在onConnectionSuspended中,尝试与mgoogleapClient.connect()重新连接;代码。感谢您的回复,但不幸的是,这不是问题所在。我的原始代码中确实有这些行,我只是忘了把它们复制/粘贴到我的帖子上。我已经编辑了这篇文章,现在应该是正确的;在任何地方,只放在一个地方我应该放在哪一个地方?onStart、onResume或onCreate?onCreate是可以的…也可以在gradle中使用此库。com.google.android.gms:play services:8.4.0请确保您正在测试具有最新google play服务的真实设备。感谢您的响应,但我正在尝试创建位置请求对象(您的第二步)。更新间隔和最快间隔在android studio中都无法识别,并导致错误。没有速效药。有什么我需要导入或执行的吗?谢谢。@Jake:我的错是那些r整数值。私有静态整数更新间隔=5000;//5秒专用静态整数测试间隔=1000;//1秒
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);