Android Can';t连接到GoogleAppClient LocationServices.API
我似乎无法让Android Can';t连接到GoogleAppClient LocationServices.API,android,geolocation,location-services,android-googleapiclient,Android,Geolocation,Location Services,Android Googleapiclient,我似乎无法让GoogleAppClient参与我的项目。我正在使用LocationServices,OnConnected未启动,即使在onStart方法中调用了client.connect()。 我已经搜索了stackoverflow并尝试了给出的解决方案,但仍然无法使其工作。 我尝试检查是否已连接,但client.isConnected()始终输出false。 我的权限是: android.permission.INTERNET android.permission.ACCESS\u位置 a
GoogleAppClient
参与我的项目。我正在使用LocationServices
,OnConnected
未启动,即使在onStart
方法中调用了client.connect()
。
我已经搜索了stackoverflow并尝试了给出的解决方案,但仍然无法使其工作。
我尝试检查是否已连接,但client.isConnected()
始终输出false。
我的权限是:
android.permission.INTERNET
android.permission.ACCESS\u位置
android.permission.ACCESS\u FINE\u位置
我是不是遗漏了什么
编辑:
试试这个
首先:删除buildGoogleAppClient();在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()+"");
}
}
}
它起作用了,但我又犯了一个错误。不确定这是否仍然相关
GoogleAppClient未配置为使用此调用所需的API。
您可以在中查看有关Google客户端位置的教程
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()+"");
}
}
}