gpt4 book ai didi

android - LocationServices.SettingsApi 使用 RESULT_CANCELED 调用 onActivityResult?

转载 作者:行者123 更新时间:2023-11-29 17:30:48 26 4
gpt4 key购买 nike

在一种特殊情况下,我一直在努力使用新的 LocationServices.SettingsApi。我已经在 SplashActivity 上编写了用于位置设置的代码(自动打开 GPS 的 Google GPS 对话框)它在 Lollipop 上运行完美,但在 Kitkat 或 Jelly Bean 等较低版本中它不显示谷歌对话框它在 onActivityResult 中显示 RESULT_CANCELED .. :(

心理急救Google 对话框图片代码 fragment Google Dialog Box for automatically turning GPS on

if(googleApiClient == null) {

googleApiClient = new GoogleApiClient.Builder(MapsActivity.this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).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); //this is the key ingredient
//**************************

PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
MapsActivity.this, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the 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;
}
}
}); }

上面的代码是自动开启gps的

下面是onActivityResult的代码

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LatLng currentLoc = LocationProvider.currentLoc;

switch (resultCode) {
case Activity.RESULT_OK:
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if(mLastLocation!=null)
{
currentLoc = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
}

Log.e(TAG, "RESULT OK");
break;
case Activity.RESULT_CANCELED:
checkGPSstatus();
Log.e(TAG, "RESULT_CANCELED");
break;
default:
break;
}

最佳答案

让我们来猜一猜。您是否为您的 Activity 将 launchMode 设置为 singleInstance?如果是这样,则您不允许其他 Activity 成为您任务的一部分。

关于android - LocationServices.SettingsApi 使用 RESULT_CANCELED 调用 onActivityResult?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33082291/

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