【问题标题】:How to solve problem with gps permissions on Android versions 12?如何解决 Android 版本 12 上的 gps 权限问题?
【发布时间】:2022-10-07 22:53:28
【问题描述】:

Android 12 及以上版本未启用 GPS 服务。 在早期版本中一切正常

我读过这个documentation, 但我不确定android:foregroundServiceType 是否有问题,因为我基本上不在应用程序中使用它。

所以这是我的一些代码AndroidManifest.xml:

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

我的虚拟机的代码:

private fun enableGps(activity: FragmentActivity) {
    val manager = activity.getSystemService(Context.LOCATION_SERVICE) as LocationManager
    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && hasGPSDevice(manager)) {
        locationManager.enableGps(activity)
    }
}

这是方法本身的实现:

override fun enableGps(activity: FragmentActivity) {
    if (googleApiClient == null) {
        googleApiClient = GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(object : GoogleApiClient.ConnectionCallbacks {
                override fun onConnected(bundle: Bundle?) {}
                override fun onConnectionSuspended(i: Int) {
                    googleApiClient?.connect()
                }
            })
            .addOnConnectionFailedListener { connectionResult ->
                Timber.e("${connectionResult.errorCode}")
            }.build()
        googleApiClient?.connect()
    }
    val locationRequest = LocationRequest.create()
    locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    locationRequest.interval = 10000
    locationRequest.fastestInterval = 5000
    val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
    builder.setAlwaysShow(true)
    googleApiClient?.let {
        val result: PendingResult<LocationSettingsResult> =
            LocationServices.SettingsApi.checkLocationSettings(it, builder.build())
        result.setResultCallback { result ->
            val status: Status = result.status
            when (status.statusCode) {
                LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> try {
                    // Show the dialog by calling startResolutionForResult(),
                    status.startResolutionForResult(
                        activity,
                        REQUEST_LOCATION
                    )
                } catch (e: IntentSender.SendIntentException) {
                    Timber.e(e)
                }
                LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                    activity.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
                }
            }
        }
    }
}

UPD这是我的DeviceLocationManager 的完整代码,也许我在这里请求我的permissions 有点错误: https://gist.github.com/mnewlive/0c0a0c1f7ccb26fe58fd6b0fa5dd1dda

【问题讨论】:

    标签: android kotlin android-permissions android-location android-gps


    【解决方案1】:

    我重写了以下方法,因为它已被弃用:

    override fun isLocationProviderActive(): Boolean {
        val providerInfo = Settings.Secure.getString(
            context.contentResolver,
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED
        )
        return providerInfo?.isNotEmpty() == true
    }
    

    override fun isLocationStateEnabled(): Boolean {
        val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        return LocationManagerCompat.isLocationEnabled(locationManager)
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-13
      • 2014-12-20
      • 1970-01-01
      • 2023-01-06
      • 1970-01-01
      • 2015-07-17
      • 2021-12-31
      • 1970-01-01
      • 2020-11-15
      相关资源
      最近更新 更多