|
|
@ -5,6 +5,7 @@ import android.app.Activity |
|
|
import android.app.ActivityManager |
|
|
import android.app.ActivityManager |
|
|
import android.content.Context |
|
|
import android.content.Context |
|
|
import android.content.pm.PackageManager |
|
|
import android.content.pm.PackageManager |
|
|
|
|
|
import android.location.LocationManager |
|
|
import android.os.Build |
|
|
import android.os.Build |
|
|
import android.os.Handler |
|
|
import android.os.Handler |
|
|
import android.os.Looper |
|
|
import android.os.Looper |
|
|
@ -150,6 +151,10 @@ object LocationService { |
|
|
callback.onLocationError(-2, "后台定位权限需要打开始终定位权限才可,请打开app重新设置权限后再使用") |
|
|
callback.onLocationError(-2, "后台定位权限需要打开始终定位权限才可,请打开app重新设置权限后再使用") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
if (!isLocationServiceEnabled(ctx)) { |
|
|
|
|
|
callback.onLocationError(-6, "定位服务未开启,请在系统设置中打开位置服务开关") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
if (!initialized) { |
|
|
if (!initialized) { |
|
|
callback.onLocationError(-3, "定位服务未初始化") |
|
|
callback.onLocationError(-3, "定位服务未初始化") |
|
|
return |
|
|
return |
|
|
@ -287,6 +292,24 @@ object LocationService { |
|
|
return 0 |
|
|
return 0 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 检查系统位置服务开关状态 |
|
|
|
|
|
* @param context 应用上下文 |
|
|
|
|
|
* @return true:位置服务已开启;false:位置服务关闭或检测失败 |
|
|
|
|
|
* @throws 无显式抛出异常(内部捕获并记录日志) |
|
|
|
|
|
*/ |
|
|
|
|
|
fun isLocationServiceEnabled(context: Context): Boolean { |
|
|
|
|
|
try { |
|
|
|
|
|
val lm = context.getSystemService(Context.LOCATION_SERVICE) as? LocationManager |
|
|
|
|
|
val gpsOn = lm?.isProviderEnabled(LocationManager.GPS_PROVIDER) == true |
|
|
|
|
|
val networkOn = lm?.isProviderEnabled(LocationManager.NETWORK_PROVIDER) == true |
|
|
|
|
|
return gpsOn || networkOn |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.e(TAG, "检测位置服务开关失败: ${e.message}", e) |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private fun hasForegroundLocationPermission(context: Context): Boolean { |
|
|
private fun hasForegroundLocationPermission(context: Context): Boolean { |
|
|
return ContextCompat.checkSelfPermission( |
|
|
return ContextCompat.checkSelfPermission( |
|
|
context, |
|
|
context, |
|
|
|