Android:既不调用onConnected,onConnectionSupspended或onConnectionFailed

时间:2016-02-15 05:26:02

标签: java android google-play-services

我正在制作一个天气应用程序,除了获取用户的位置外,一切正常。我正在尝试使用play服务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();
}

2 个答案:

答案 0 :(得分:1)

您错过了初始化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。

Fused location provider example

答案 1 :(得分:0)

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