如何正确提示用户打开“提高位置准确性”设置?

Sha*_*han 3 android

我的应用程序要求用户在其设置中启用“提高的位置准确性”。不幸的是,我无法找出正确的方法来真正进入正确的菜单,并且一旦设置完成就让后退按钮返回我的应用程序。

这是我所拥有的:

    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
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

San*_*ani 5

下面的代码用于提示用户更改位置设置 单击此处查看完整的文档

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.
     }
 }
}
Run Code Online (Sandbox Code Playgroud)