【问题标题】:LocationSettings Dialog Appears even if GPS turn on即使打开 GPS 也会出现 LocationSettings 对话框
【发布时间】:2019-04-22 12:03:13
【问题描述】:
val request = LocationRequest()
    request.interval = 1000 * 60
    request.fastestInterval = 1000 * 30
    request.smallestDisplacement = 10f
    request.priority = LocationRequest.PRIORITY_HIGH_ACCURACY

    val builder = LocationSettingsRequest.Builder().addLocationRequest(request)
    builder.setAlwaysShow(true)
    val result = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build())
    result.addOnFailureListener {
        if (it 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().
                it.startResolutionForResult(this, 1)
            } catch (sendEx: IntentSender.SendIntentException) {
                // Ignore the error.
            }

        }

    }

以上代码用于要求用户打开位置。但是最近我发现有时即使打开了位置,它也会要求打开位置。

编辑:1 我最近发现,如果设备开启了省电模式或设备位置精度设置为 LOW,那么此请求将失败并显示相同的状态代码。

【问题讨论】:

    标签: android location


    【解决方案1】:

    OnFailureListener 中,您还可以检查 GPS 是否打开或关闭,然后显示用户对话框。像这样:

    result.addOnFailureListener {
        if (it is ResolvableApiException) {
            try {
               val lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
               val gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
               if(!gps_enabled) {
                   it.startResolutionForResult(this, 1)
               } else {
                   powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                   val locationMode = Settings.Secure.getInt(activityUnderTest.getContentResolver(), Settings.Secure.LOCATION_MODE)
    
                // location mode status:
                //  0 = LOCATION_MODE_OFF
                //  1 = LOCATION_MODE_SENSORS_ONLY
                //  2 = LOCATION_MODE_BATTERY_SAVING
                //  3 = LOCATION_MODE_HIGH_ACCURACY
    
                   if(powerManager.powerSaverMode) {
                       //show dialog to turn off the battery saver mode
                   } else if (locationMode != 3){
                      //show dialog that "make sure your location accuracy is not low"
                   }
               }
            } catch (sendEx: Exception) {
                // Ignore the error.
            }
    
        }
    
    }
    

    请记住,您需要在清单文件中添加以下权限。

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    

    【讨论】:

    • 如果启用了GPS,其他部分应该怎么做?
    • @LalitJadav 您可以向 toast 显示“GPS 没有响应,请确保您未处于省电模式”
    • 如果用户点击“不,谢谢”,收到相同的回复,我想强制开启 gps。
    • @LalitJadav 在这种情况下您可以关闭应用程序,也许用户此时出于其他原因不想使用 GPS。如果 GPS 没有响应,您只能提示用户此应用将无法正常工作。
    • 请检查编辑,它只发生在某些情况下,即使用户希望它启用并点击确定
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多