gpt4 book ai didi

android - 无法连接到 GoogleAPIClient LocationServices.API

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

我似乎做不到GoogleApiClient从事我的项目。我正在使用 LocationServices , OnConnected即使我有 client.connect() 也没有开火在 onStart 中调用方法。我搜索了 stackoverflow 并尝试了给出的解决方案,但我仍然无法使其工作。我试着检查我是否已连接但是client.isConnected()总是输出假。我的权限是:

android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION

我在这里遗漏了什么吗?

编辑:

private GoogleApiClient client;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ButterKnife.inject(this);

buildGoogleApiClient();
}

protected synchronized void buildGoogleApiClient() {
client = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}

@Override
public void onStart() {
// client.connect();
super.onStart();

// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Log.e("Connected?", String.valueOf(client.isConnected()));

}

public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e("Failed?", connectionResult.getErrorMessage());
}


@Override
public void onConnected(Bundle bundle) {
if ( ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
client);
longt = mLastLocation.getLongitude();
latt = mLastLocation.getLatitude();
if (mLastLocation != null) {
// mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
// mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}

}

最佳答案

试试这个!!

首先:删除 buildGoogleApiClient();在 Oncreate() 中;

然后:

public static final int REQUEST_CODE_RESOLUTION = 1;

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

private void buildGoogleClient(){
if(client != null ){
client = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
}
client.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
client.connect();

// Enter code get Latude and Longtude

mLastLocation = LocationServices.FusedLocationApi.getLastLocation(client);
longt = mLastLocation.getLongitude();
latt = mLastLocation.getLatitude();
if (mLastLocation != null) {
mLatitudeText.setText(mLastLocation.getLatitude()+"");
mLongitudeText.setText(mLastLocation.getLongitude()+"");
}
}
}

关于android - 无法连接到 GoogleAPIClient LocationServices.API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37245045/

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