Browse Source

上传后台定位权限说明

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
fa035eaac1
  1. 39
      local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationService.kt

39
local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationService.kt

@ -141,12 +141,15 @@ object LocationService {
callback.onLocationError(-1, "定位服务未初始化")
return
}
if (!checkPermissions(ctx)) {
callback.onLocationError(-2, "没有定位权限")
val p = checkPermissions(ctx)
if (p == 1) {
callback.onLocationError(-2, "定位权限没有")
return
}
if (p == 2) {
callback.onLocationError(-2, "后台定位权限没有")
return
}
if (!initialized) {
callback.onLocationError(-3, "定位服务未初始化")
return
@ -238,7 +241,7 @@ object LocationService {
override fun run() {
val ctx = appcontext ?: return
val hasPermission = checkPermissions(ctx)
if (hasPermission && !initialized) {
if (hasPermission ==0 && !initialized) {
Log.d(TAG, "权限已授予,重新初始化定位服务")
initialize(ctx)
stopPermissionMonitor()
@ -256,12 +259,26 @@ object LocationService {
permissionRetryRunnable = null
}
private fun checkPermissions(context: Context): Boolean {
return if (isAppInForeground(context)) {
hasForegroundLocationPermission(context)
} else {
hasBackgroundLocationPermission(context)
/**
* 检查位置权限状态并返回对应编码
* @return 0:已获得所需全部权限;1:未获得前台权限;2:前台权限已获得但后台权限未授予(仅Android 10+)
*/
private fun checkPermissions(context: Context): Int {
// 先检查前台权限是否已授予
val hasForegroundPermission = hasForegroundLocationPermission(context)
if (!hasForegroundPermission) {
return 1 // 前台权限未授予
}
// 前台权限已授予,再根据前后台状态检查后台权限
if (!isAppInForeground(context)) {
// 应用在后台:Android 10+ 需要检查后台权限
return 2 // 后台权限未授予
}
// 所有所需权限均已授予
return 0
}
private fun hasForegroundLocationPermission(context: Context): Boolean {
@ -306,7 +323,7 @@ object LocationService {
val ctx = appcontext ?: activity
// 如果已经有权限,直接返回成功
if (checkPermissions(ctx)) {
if (checkPermissions(ctx) == 0) {
result.success(mapOf("granted" to true))
return
}

Loading…
Cancel
Save