为什么resultCode
中来自onActivityResult
的{em> Location Request 使用Google API客户端始终为零?
连接到API客户端的Android移动应用程序:
final static int REQUEST_LOCATION =2001;
private void getGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
定义位置请求:
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(30000);
mLocationRequest.setFastestInterval(10000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Loc_Update();
}
使用Google APi客户端请求位置
private void Loc_Update() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest)
.setAlwaysShow(true);
Task<LocationSettingsResponse> task = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
//Do what you want with location
//like update camera
// mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16f));
}
}
},null);
}
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try {
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(MainActivity.this, 2001);
break;
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
} catch (ClassCastException e) {
// Ignore, should be an impossible error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}}
});
task.addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
}
});
}
//GoogleApiClient.ConnectionCallbacks
public void onConnected(Bundle bundle) {
createLocationRequest();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(getApplicationContext(),"Failed to connect ",Toast.LENGTH_SHORT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
switch (requestCode)
{
case REQUEST_LOCATION:
switch (resultCode)
{
case Activity.RESULT_OK:
{
Toast.makeText(getApplicationContext(),"user responded yes ",Toast.LENGTH_SHORT);
break;
}
case Activity.RESULT_CANCELED:
{
Toast.makeText(getApplicationContext(),"user responded No ",Toast.LENGTH_SHORT);
break;
}
default:
{
break;
}
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
单击“确定”时,resultCode
应该为-1,“不谢谢”时,resultCode
应该为0