gpt4 book ai didi

android - 在 Android 10 中,当以编程方式打开位置时,我得到结果代码 RESULT_CANCELED

转载 作者:行者123 更新时间:2023-12-02 10:20:51 25 4
gpt4 key购买 nike

Android 10 中,以编程方式访问位置时,会显示一个打开位置的弹出窗口,按下“确定”(即打开位置)后,就会在 onActivityResult 中回调> 我得到的结果代码是 Activity.RESULT_CANCELED 而不是 Activity.RESULT_OK。手机中的定位功能也已打开。

这在 Android 10 以下工作正常。只是在 Android 10 中不起作用。我得到的结果代码是这样的 -Activity.RESULT_CANCELED

enter image description here

启用位置代码

activity.setFinishOnTouchOutside(true);

final int REQUEST_LOCATION = 199;

GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity)
.addApi(LocationServices.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.e("location", "Connect");
}

@Override
public void onConnectionSuspended(int i) {
Log.e("location", "fail");
//googleApiClient.connect();
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("location", "Location error " + connectionResult.getErrorCode());
}
}).build();
googleApiClient.connect();

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);

SettingsClient client = LocationServices.getSettingsClient(activity);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(activity, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
Log.d("location_enable", "enable");
}
});

task.addOnFailureListener(activity, 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(activity,
REQUEST_LOCATION);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});

获取结果onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_LOCATION) {
switch (resultCode) {
case Activity.RESULT_OK:
//Success Perform Task Here
getLocation();
break;
case Activity.RESULT_CANCELED:
progressDialog.dismiss();
method.alertBox(getResources().getString(R.string.please_allow_location));
break;
}
}
}

AndroidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

最佳答案

这是一个错误,这是 google 问题跟踪器 https://issuetracker.google.com/issues/140447198 上的链接

同时,您可以在 onActivityResult 中仔细检查 GPS 状态,以解决 Android 10 中的此问题

关于android - 在 Android 10 中,当以编程方式打开位置时,我得到结果代码 RESULT_CANCELED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58027878/

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