我阅读了文档,并且看到了很多有关此问题的问答,但是我找不到任何东西。我将startResolutionForResult用于“位置”设置,就像在文档中写的一样。 代码看起来很简单,我的清单文件中只有一个活动MainActivity,没有片段,ACCESS_FINE_LOCATION权限。它工作正常,但onActivityResult的resultCode始终返回0。 谢谢您的帮助。
createAndCheckLocationRequest();
}
private void createAndCheckLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// requestLocationUpdate();
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MainActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CHECK_SETTINGS){
if(resultCode == Activity.RESULT_OK){
// requestLocationUpdate();
Toast.makeText(this,"*** OK ****",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"Location request not satisfied",Toast.LENGTH_SHORT).show();
}
}
}
}