如何在 android 中显示位置设置对话框?

how to show location setting dialog in android?

从 google i/o 2015 年,我了解到 google 播放服务中有一个新对话框,用户无需退出当前应用程序即可打开位置。下图显示了它的外观:

现在我的问题是,如何在我的项目中实现它?我已搜索但未找到任何有效答案,请帮助!

看看 google service documetation 的 api 你会发现一切都有据可查。 根据您的要求,我建议使用 LocationSettingsRequest.Builder 来达到您的目标。

我在Whosebug中找到了一个Kai的例子:

使用 Kotlin

通过进行以下一项更改,此解决方案适用于 ActivityFragment

为ActivityresolvableApiException.startResolutionForResult(this@MainActivity, REQUEST_CHECK_SETTING)

对于片段 startIntentSenderForResult(resolvableApiException.resolution.intentSender, REQUEST_CHECK_SETTING, null, 0, 0,0,null)

通过使用LocationSettingsResponse可以完成这个任务。

里面MainActivity.kt


    private fun checkLocationSetting()
        {
            locationRequest = LocationRequest.create()
            locationRequest.apply {
                priority=LocationRequest.PRIORITY_HIGH_ACCURACY
                interval = 5000
                fastestInterval = 2000
            }
    
            val builder = LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest)
            builder.setAlwaysShow(true)
    
            val result: Task<LocationSettingsResponse> = LocationServices.getSettingsClient(applicationContext)
                .checkLocationSettings(builder.build())
    
            result.addOnCompleteListener {
                try{
                    val response: LocationSettingsResponse = it.getResult(ApiException::class.java)
                    Toast.makeText(this@MainActivity, "GPS is On", Toast.LENGTH_SHORT).show()
                    Log.d(TAG, "checkSetting: GPS On")
                }catch(e:ApiException){
    
                    when(e.statusCode){
                        LocationSettingsStatusCodes.RESOLUTION_REQUIRED ->{
                            val resolvableApiException = e as ResolvableApiException
// for fragment change below line to: startIntentSenderForResult(resolvableApiException.resolution.intentSender, REQUEST_CHECK_SETTING, null, 0, 0,0,null)
                            resolvableApiException.startResolutionForResult(this@MainActivity, REQUEST_CHECK_SETTING)
                            Log.d(TAG, "checkSetting: RESOLUTION_REQUIRED")
                        }
    
                        LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                            // USER DEVICE DOES NOT HAVE LOCATION OPTION
                        }
                    }
                }
            }
        }

上Activity结果

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode)
        {
            REQUEST_CHECK_SETTING ->{
                when(resultCode){
                    Activity.RESULT_OK->{
                        Toast.makeText(this@MainActivity, "GPS is Turned on", Toast.LENGTH_SHORT).show()
                    }
                    Activity.RESULT_CANCELED ->{
                        Toast.makeText(this@MainActivity, "GPS is Required to use this app", Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }

Link 完成代码 MainActivity.kt

输出:

Link 完成代码 MainActivity.kt