gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:33:35 27 4
gpt4 key购买 nike

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

最佳答案

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

 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

请引用以下链接获取位置信息。

Fused location provider example

关于java - Android:onConnected、onConnectionSupspended 或 onConnectionFailed 都没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com