cloudroam
2025-02-14 2fce91b8c0faf1290d8a35ee022dab3cdbc28a54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.example.firstapp.utils
 
import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Build
import android.provider.Settings
import androidx.core.content.ContextCompat
 
@Suppress("DEPRECATION")
object LocationUtils {
 
    private const val LOCATION_MODE_OFF = 0
 
    private fun hasLocationPermission(context: Context): Boolean {
        val hasPermission = ContextCompat.checkSelfPermission(
            context,
            android.Manifest.permission.ACCESS_FINE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED
        com.example.firstapp.utils.Log.d("LocationUtils", "hasLocationPermission: $hasPermission")
        return hasPermission
    }
 
    fun isLocationEnabled(context: Context): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager?
            com.example.firstapp.utils.Log.d(
                "LocationUtils",
                "isLocationEnabled: ${locationManager?.isLocationEnabled}"
            )
            locationManager?.isLocationEnabled == true
        } else {
            try {
                val locationMode = Settings.Secure.getInt(
                    context.contentResolver,
                    Settings.Secure.LOCATION_MODE
                )
                com.example.firstapp.utils.Log.d(
                    "LocationUtils",
                    "isLocationEnabled: locationMode=$locationMode"
                )
                locationMode != com.example.firstapp.utils.LocationUtils.LOCATION_MODE_OFF
            } catch (e: Settings.SettingNotFoundException) {
                false
            }
        }
    }
 
    fun hasLocationCapability(context: Context): Boolean {
        val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager?
 
        // 检查是否有位置权限
        if (!com.example.firstapp.utils.LocationUtils.hasLocationPermission(context)) {
            com.example.firstapp.utils.Log.e(
                "LocationUtils",
                "hasLocationCapability: no location permission"
            )
            return false
        }
 
        // 检查是否有定位能力
        val hasGpsProvider = locationManager?.isProviderEnabled(LocationManager.GPS_PROVIDER) == true
        val hasNetworkProvider = locationManager?.isProviderEnabled(LocationManager.NETWORK_PROVIDER) == true
        val hasPassiveProvider = locationManager?.isProviderEnabled(LocationManager.PASSIVE_PROVIDER) == true
 
        com.example.firstapp.utils.Log.d(
            "LocationUtils",
            "hasLocationCapability: hasGpsProvider=$hasGpsProvider, hasNetworkProvider=$hasNetworkProvider, hasPassiveProvider=$hasPassiveProvider"
        )
        return hasGpsProvider || hasNetworkProvider || hasPassiveProvider
    }
}