如何正确提示用户开启“提高定位精度”设置?
Posted
技术标签:
【中文标题】如何正确提示用户开启“提高定位精度”设置?【英文标题】:How to correctly prompt a user to turn on "Improve Location Accuracy" setting? 【发布时间】:2019-11-11 18:47:36 【问题描述】:我的应用程序要求用户在其设置中启用改进的位置准确性。不幸的是,我无法找出正确的方法来实际进入正确的菜单,并在设置完成后让后退按钮返回到我的应用程序。
这是我所拥有的:
private fun checkLocationSettingsAsync()
viewModel.launch(Dispatchers.IO)
val locationSettings = client.checkLocationSettings(
locationBuilder.build()
).asDeferred()
locationSettings.asTask().addOnSuccessListener
//location settings are already high accuracy
locationSettingsOkay = true
.addOnFailureListener e ->
if (e is ResolvableApiException)
//location settings not satisfied, request to make them higher
runCatching
val intent = Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivityForResult(intent, LOCATION_ACCURACY_ACCESS_CODE)
.onFailure
println(it.stackTrace)
locationSettings.await()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
super.onActivityResult(requestCode, resultCode, data)
when (requestCode)
LOCATION_ACCURACY_ACCESS_CODE ->
if (resultCode == Activity.RESULT_OK)
//TODO: navigate back to activity once setting is set
Android 菜单 Settings.ACTION_LOCATION_SOURCE_SETTINGS
进入包含改进的位置精度设置的外部菜单,但我实际上无法找到正确的 Intent 名称来进入相应的菜单,我也无法获取它以便用户可以启用设置后导航回我的应用程序。
有什么建议吗?或文档,因为我找不到任何关于此的内容!
编辑:这是一个很好的代码示例! https://github.com/googlesamples/android-play-location/blob/master/LocationUpdates/app/src/main/java/com/google/android/gms/location/sample/locationupdates/MainActivity.java
【问题讨论】:
【参考方案1】:以下代码用于提示用户更改位置设置 Click here to see full document
task.addOnSuccessListener locationSettingsResponse ->
// All location settings are satisfied. The client can initialize
// location requests here.
task.addOnFailureListener exception ->
if (exception is ResolvableApiException)
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
exception.startResolutionForResult(this@MainActivity,
REQUEST_CHECK_SETTINGS)
catch (sendEx: IntentSender.SendIntentException)
// Ignore the error.
【讨论】:
很有帮助,谢谢!不幸的是,我正在尝试使用 startIntentSenderForResult 从片段启动分辨率,而不是 startResolutionForResult【参考方案2】:我是这样做的
Gps 设置不正常?
-
使用 显示用户“打开 Gps 对话框”
来自活动:
status.startResolutionForResult(this , REQUEST_CHECK_SETTINGS);
来自片段:
startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CHECK_SETTINGS, null, 0, 0, 0, null)
-
在
onActivityResult
中查找结果
如果您请求 HIGH_ACCURACY 升级,请注意此错误
Not Able To Upgrade Gps settings to high Accuracy using FusedLocationProvider In some devices
虽然我在上次更新 Google Play 服务后没有遇到这个错误.....但是我不确定这个错误是否已经完全修复
如果 resultCode 为 RESULT_OK -> 什么也不做
如果 resultCode 是 RESULT_CANCELED -> 显示用户需要手动升级 Gps 优先级的对话框 并使用
将用户重定向到位置设置页面Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, REQUEST_UPDATE_GPS_SETTINGS_MANUALLY);
【讨论】:
【参考方案3】:这可能会帮助你:
对不起,如果它是在 java 中。让我知道它是否有效。
public static void displayPromptForEnablingGPS(final Activity activity, Context context)
LocationManager lm = (LocationManager) activity.getSystemService(LOCATION_SERVICE);
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.locationTitle);
builder.setMessage(R.string.enableLocation);
builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialogInterface, int i)
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivity(intent);
);
Dialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
【讨论】:
【参考方案4】:这是我在意识到我需要使用startIntentSenderForResult
而不是startResolutionForResult
后提出的解决方案,因为我是从片段中启动它的。 :-)
private fun checkLocationSettings()
settingsClient.checkLocationSettings(locationSettingsRequest).addOnSuccessListener
//all settings satisfied!
locationSettingsOkay = true
.addOnFailureListener e ->
val statusCode = (e as ApiException).statusCode
when (statusCode)
LocationSettingsStatusCodes.RESOLUTION_REQUIRED ->
println("settings not good enough, attempting to upgrade them")
try
val rae = (e as ResolvableApiException)
startIntentSenderForResult(
rae.resolution.intentSender,
REQUEST_CHECK_SETTINGS,
null, 0, 0, 0, null
)
catch (sie : IntentSender.SendIntentException)
println("pending intent unable to process request")
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE ->
Toast.makeText(
this@PrimerFrag.requireContext(),
"Location settings can't be fixed here. Try to turn on Improved Location Accuracy in settings.",
Toast.LENGTH_LONG
).show()
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
when (requestCode)
REQUEST_CHECK_SETTINGS ->
when (resultCode)
Activity.RESULT_OK ->
locationSettingsOkay = true
if (locationPermissionGranted && resultSuccess)
makeButtonsReadyAndSubmitList(resultList, destinationsAdapter)
Activity.RESULT_CANCELED ->
//TODO: ask for location settings again
【讨论】:
以上是关于如何正确提示用户开启“提高定位精度”设置?的主要内容,如果未能解决你的问题,请参考以下文章