目前受影响的设备:
Use:为了请求位置更新,我检查位置设置
事先就足够了。如果没有,我会显示一条小文字,表明服务必须
为我的功能启用。如果用户单击它,系统对话框将启用
会提示定位服务。
我如何运行检查:我检查完毕LocationSettingsRequest
and LocationServices
和
处理ResolvableApiException
。如果用户禁用该服务,
然后在我的所有设备上,这将显示系统对话框,要求用户启用
服务(并启用它,如果单击“确定”)。
Redmi 上会发生什么:但不管出于什么原因,在红米Note 7上,checkLocationSettings
will always返回一个ResolvableApiException
,即使服务
已经启用。对话won't出现并直接返回阳性结果
(当然,因为该服务已启用)。
因此,用户被卡住并永远单击“启用”。
有人知道为什么LocationSettingsRequest
不管用
正确地安装在这些设备上并知道如何修复它?
示例代码片段
void enableLocationSettings(onLocationServiceRequiredCallback: ICallback) {
LocationRequest locationRequest = LocationRequest.create()
.setInterval(LOCATION_UPDATE_INTERVAL)
.setExpirationDuration(LOCATION_UPDATE_EXPIRATION_DURATION)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
LocationServices
.getSettingsClient(this)
.checkLocationSettings(builder.build())
.addOnSuccessListener(this, (LocationSettingsResponse response) -> {
// update location as usual
})
.addOnFailureListener(this, error -> {
if (error instanceOf ApiException) {
int statusCode = error.statusCode;
if(statusCode == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
if(error instanceOf ResolvableApiException) {
PendingIntent resolution = error.resolution
// This will trigger the system dialog, see below
onLocationServiceRequiredCallback(resolution);
}
}
}
// error callback
});
不幸的是,回调的代码现在将是 kotlin。对此感到抱歉。我只能发布“示例代码片段”,因为这是与工作相关的。
It uses ActivityResultContracts.StartIntentSenderForResult
开始
系统对话框。
resolution
是上面的 PendingIntent。
合同:
private val locationServiceContract = registerForActivityResult(
ActivityResultContracts.StartIntentSenderForResult()
) { activityResult ->
lifecycleScope.launchWhenResumed {
val result = if (activityResult.resultCode == Activity.RESULT_OK) {
// This will retrigger the location fetch (looping)
enableLocationSettings(...)
} else {
// Do nothing
}
}
}
调用上面的回调后,将执行:
locationServiceContract.launch(
IntentSenderRequest.Builder(
resolution
).build()
)
Dump of LocationSettingsStates
,使用已弃用的 LocationSettings API(使用 GoogleApiClient)获取:
isGpsPresent: true,
isGpsUsable: false,
isLocationPresent: true,
isLocationUsable: true,
isNetworkLocationPresent: true,
isNetworkLocationUsable: true