From 36e5d817cdd3cbf6e8cf0e37e21782eb08abdd46 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Sat, 21 Jun 2025 11:22:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=AE=9A=E4=BD=8D=E8=87=AA?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../location_service/LocationService.kt | 113 +++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) diff --git a/local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationService.kt b/local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationService.kt index 5a2319917..8e25fdea2 100644 --- a/local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationService.kt +++ b/local_plugins/location_service/android/src/main/kotlin/com/yunqiinnovation/location_service/LocationService.kt @@ -1,23 +1,61 @@ package com.yunqiinnovation.location_service +import android.Manifest +import android.app.ActivityManager import android.content.Context +import android.content.pm.PackageManager import android.location.Location +import android.os.Build +import android.os.Handler +import android.os.Looper import android.util.Log +import androidx.core.content.ContextCompat import com.amap.api.location.* import io.flutter.plugin.common.MethodChannel object LocationService { private const val TAG = "LocationService" - + private var appcontext:Context? = null private var locationClient: AMapLocationClient? = null private var currentLocation: AMapLocation? = null private var initialized = false + private var permissionRetryHandler: Handler? = null + private var permissionRetryRunnable: Runnable? = null + + /** + * 检查应用是否在前台运行 + * @return 应用是否在前台 + */ + fun isAppInForeground(context: Context,): Boolean { + try { + val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + val appProcesses = activityManager.runningAppProcesses ?: return false + val packageName = context.packageName + + for (appProcess in appProcesses) { + if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND + && appProcess.processName == packageName) { + return true + } + } + } catch (e: Exception) { + Log.e("ChatApi", "检查应用状态失败: ${e.message}", e) + } + return false + } + fun initialize(context: Context) { if (initialized) return + appcontext = context // Log.e(TAG, "liwei--------- 初始化高德定位服务 1") - + if (!checkPermissions(context)) { + Log.w(TAG, "没有定位权限,等待用户授权中...") + startPermissionMonitor() + return + } try { + AMapLocationClient.updatePrivacyShow(context, true, true) AMapLocationClient.updatePrivacyAgree(context, true) // Log.e(TAG, "liwei--------- 初始化高德定位服务 2") @@ -47,6 +85,61 @@ object LocationService { } } + + + private fun startPermissionMonitor() { + if (permissionRetryHandler != null) return + + permissionRetryHandler = Handler(Looper.getMainLooper()) + permissionRetryRunnable = object : Runnable { + override fun run() { + val ctx = appcontext ?: return + val hasPermission = checkPermissions(ctx) + if (hasPermission && !initialized) { + Log.d(TAG, "权限已授予,重新初始化定位服务") + initialize(ctx) + stopPermissionMonitor() // 停止定时器 + } else { + permissionRetryHandler?.postDelayed(this, 5000) + } + } + } + permissionRetryHandler?.post(permissionRetryRunnable!!) + } + + private fun stopPermissionMonitor() { + permissionRetryHandler?.removeCallbacks(permissionRetryRunnable!!) + permissionRetryHandler = null + permissionRetryRunnable = null + } + + private fun checkPermissions(context: Context): Boolean { + if (isAppInForeground(context)) { + return hasForegroundLocationPermission(context) + }else{ + return hasBackgroundLocationPermission(context) + } + } + + private fun hasForegroundLocationPermission(context: Context): Boolean { + return ContextCompat.checkSelfPermission( + context, + Manifest.permission.ACCESS_FINE_LOCATION + ) == PackageManager.PERMISSION_GRANTED + } + + private fun hasBackgroundLocationPermission(context: Context): Boolean { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + ContextCompat.checkSelfPermission( + context, + Manifest.permission.ACCESS_BACKGROUND_LOCATION + ) == PackageManager.PERMISSION_GRANTED + } else { + true // Android 9 及以下没有这个权限 + } + } + + fun getCachedLocation(): Map? { // // 定位成功,获取经纬度 // val latitude = location.latitude @@ -60,6 +153,22 @@ object LocationService { // val streetNum = location.streetNum // 门牌号 // val adCode = location.adCode // 行政区划代码 // val address = location.address // 详细地址拼接字符串 + if (!initialized){ + if (checkPermissions(appcontext!!)) { + return mapOf( + "user_location" to "当前定位权限为获取", + "user_lat" to "当前定位权限为获取", + "user_lng" to "当前定位权限为获取", + ) + }else{ + return mapOf( + "user_location" to "后台服务下,需要后台定位权限", + "user_lat" to "后台服务下,需要后台定位权限", + "user_lng" to "后台服务下,需要后台定位权限", + ) + } + } + return currentLocation?.let { mapOf( "user_location" to it.address,