gpt4 book ai didi

android - 如果禁用定位服务,getFusedLocationProviderClient 不会激活我的 GPS

转载 作者:行者123 更新时间:2023-11-29 23:54:18 29 4
gpt4 key购买 nike

我使用网络代码获取 gps 位置:

    mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);


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

SettingsClient settingsClient = LocationServices.getSettingsClient(this);
settingsClient.checkLocationSettings(locationSettingsRequest);

getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
onLocationChanged(locationResult.getLastLocation());
}
},
Looper.myLooper());

一切正常,除了如果我的设备停用了定位服务,getFusedLocationProviderClient 会打开一个对话框并告诉该设备需要 wifi、手机网络和传感器。我同意并且应用程序激活了我的位置服务,但使用低功率设置(省电模式)。

我做错了什么?看起来 GPS 传感器没有激活,因为我的位置显示离我的真实位置很远。如果我进行手动设置,所有设置都非常有效。

最佳答案

解决了我的问题!

Task<LocationSettingsResponse> result;

......................

getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
onLocationChanged(locationResult.getLastLocation());
}
},
Looper.myLooper());

这也激活了 GPS:

result = LocationServices.getSettingsClient(this).checkLocationSettings(locationSettingsRequest);
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvable = (ResolvableApiException) exception;
resolvable.startResolutionForResult(
NewMainActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
//
} catch (ClassCastException e) {
//
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
}
});

关于android - 如果禁用定位服务,getFusedLocationProviderClient 不会激活我的 GPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50542661/

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