gpt4 book ai didi

android - 不使用 Play Services API 启用位置/GPS 设置

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:24:57 24 4
gpt4 key购买 nike

大多数人可能已经知道,Google Play 服务中有一个新的 Location Settings Dialog API。

对于那些不知道的人,这里是使用 Dialog API 的 Maps 应用程序的屏幕截图。

Google Maps Location Dialog

Google 仅在其播放服务中添加该 API 有点不公平。另一方面,我认为如果他们能做到,就会有解决方法。这就是我问这个问题的原因。任何信息表示赞赏。

(注意:有一个漏洞可以让开发人员以编程方式启用和禁用 gps/位置设置。但是,那些在 SO 上可用的技巧不再有效。请不要发布过时的答案。)

最佳答案

嗨,这对我有用。

void initApiClient() {
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
showLocationSettingsDialog();
}

@Override
public void onConnectionSuspended(int i) {

}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
Log.i(TAG, "onConnectionFailed() connectionResult = [" + result + "]");
}
})
.build();
mGoogleApiClient.connect();
}

void showLocationSettingsDialog() {

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);

Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getActivity())
.checkLocationSettings(builder.build());

result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
} catch (ApiException e) {
switch (e.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) e;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(
getActivity(),
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException exception) {
// Ignore the error.
} catch (ClassCastException exception) {
// 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;
}
}
}
});
}

more info

关于android - 不使用 Play Services API 启用位置/GPS 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022133/

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