|
|
@ -1,23 +1,61 @@ |
|
|
package com.yunqiinnovation.location_service |
|
|
package com.yunqiinnovation.location_service |
|
|
|
|
|
|
|
|
|
|
|
import android.Manifest |
|
|
|
|
|
import android.app.ActivityManager |
|
|
import android.content.Context |
|
|
import android.content.Context |
|
|
|
|
|
import android.content.pm.PackageManager |
|
|
import android.location.Location |
|
|
import android.location.Location |
|
|
|
|
|
import android.os.Build |
|
|
|
|
|
import android.os.Handler |
|
|
|
|
|
import android.os.Looper |
|
|
import android.util.Log |
|
|
import android.util.Log |
|
|
|
|
|
import androidx.core.content.ContextCompat |
|
|
import com.amap.api.location.* |
|
|
import com.amap.api.location.* |
|
|
import io.flutter.plugin.common.MethodChannel |
|
|
import io.flutter.plugin.common.MethodChannel |
|
|
|
|
|
|
|
|
object LocationService { |
|
|
object LocationService { |
|
|
private const val TAG = "LocationService" |
|
|
private const val TAG = "LocationService" |
|
|
|
|
|
private var appcontext:Context? = null |
|
|
private var locationClient: AMapLocationClient? = null |
|
|
private var locationClient: AMapLocationClient? = null |
|
|
private var currentLocation: AMapLocation? = null |
|
|
private var currentLocation: AMapLocation? = null |
|
|
private var initialized = false |
|
|
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) { |
|
|
fun initialize(context: Context) { |
|
|
if (initialized) return |
|
|
if (initialized) return |
|
|
|
|
|
appcontext = context |
|
|
// Log.e(TAG, "liwei--------- 初始化高德定位服务 1") |
|
|
// Log.e(TAG, "liwei--------- 初始化高德定位服务 1") |
|
|
|
|
|
if (!checkPermissions(context)) { |
|
|
|
|
|
Log.w(TAG, "没有定位权限,等待用户授权中...") |
|
|
|
|
|
startPermissionMonitor() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
try { |
|
|
try { |
|
|
|
|
|
|
|
|
AMapLocationClient.updatePrivacyShow(context, true, true) |
|
|
AMapLocationClient.updatePrivacyShow(context, true, true) |
|
|
AMapLocationClient.updatePrivacyAgree(context, true) |
|
|
AMapLocationClient.updatePrivacyAgree(context, true) |
|
|
// Log.e(TAG, "liwei--------- 初始化高德定位服务 2") |
|
|
// 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<String, String>? { |
|
|
fun getCachedLocation(): Map<String, String>? { |
|
|
// // 定位成功,获取经纬度 |
|
|
// // 定位成功,获取经纬度 |
|
|
// val latitude = location.latitude |
|
|
// val latitude = location.latitude |
|
|
@ -60,6 +153,22 @@ object LocationService { |
|
|
// val streetNum = location.streetNum // 门牌号 |
|
|
// val streetNum = location.streetNum // 门牌号 |
|
|
// val adCode = location.adCode // 行政区划代码 |
|
|
// val adCode = location.adCode // 行政区划代码 |
|
|
// val address = location.address // 详细地址拼接字符串 |
|
|
// 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 { |
|
|
return currentLocation?.let { |
|
|
mapOf( |
|
|
mapOf( |
|
|
"user_location" to it.address, |
|
|
"user_location" to it.address, |
|
|
|