gpt4 book ai didi

android - isGooglePlayServicesAvailable 和 LocationClient

转载 作者:太空狗 更新时间:2023-10-29 16:18:39 29 4
gpt4 key购买 nike

是否可以使用 GooglePlayServicesLocationClient?

最佳答案

我想我来不及回答这个问题了。但没关系。因此,借助新的 API LocationSettingsRequest,我们只需单击一下即可在应用程序中启用位置。所以这样做;初始化 GoogleApiClient

        GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();

然后按如下方式初始化 LocationRequest:

LocationRequest mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(Util.UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(Util.FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
}

现在通过 LocationSettingsRequest 发出位置设置请求。

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);

//**************************
builder.setAlwaysShow(true); //this is the key ingredient
//**************************

PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(locationSettingsResultCallback);

因此,一旦您发出 LocationSettingsRequest,它就会弹出如下对话框:

enter image description here

因此,要处理用户选择的任何选项,您必须实现回调来处理每个选项,如下所示:

ResultCallback<LocationSettingsResult> locationSettingsResultCallback = 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.
//startLocationUpdates();


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(
PickupLocationActivity.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;
}
}
};

引用: https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi

关于android - isGooglePlayServicesAvailable 和 LocationClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21181516/

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