Work Manager 的位置更新不起作用

Location Update from Work Manager Doesn't work

最近我一直在尝试实现逻辑,以便使用工作管理器从 FusedLocationProviderClient 获取位置更新,每 15 分钟一次(因为这是最小后台执行延迟)。但是,FusedLocationProviderClient 的 requestLocationUpdates() 函数每 15 分钟失败一次,我无法从中获得成功结果任务,我总是得到失败的结果。当我从 fusedlocationproviderclient 打印异常时,它只打印“null”?有帮助吗?

片段:

private fun addWorker(){
        val workRequest = PeriodicWorkRequestBuilder<MyWorker>(15, TimeUnit.MINUTES)
            .build()
        val workManager = WorkManager.getInstance(requireContext())
        workManager.enqueueUniquePeriodicWork(
            "MyMainWorker",
            ExistingPeriodicWorkPolicy.KEEP,
            workRequest
        )
}

工人

class MyWorker(context: Context, wp: WorkerParameters) :
    Worker(context, wp) {

    private val LOCATION_UPDATE_INTERVAL = TimeUnit.MINUTES.toMillis(4)
    private val LOCATION_FASTEST_UPDATE_INTERVAL = TimeUnit.MINUTES.toMillis(2)

    private var ctx: Context = context

    override fun doWork(): Result {
        return if (getLocationUpdate()) {
            Log.d("MyWorker", "SUCCESS")
            Result.success()
        } else {
            Log.d("MyWorker", "FAILURE")
            Result.failure()
        }
    }

    @SuppressLint("MissingPermission")
    private fun getLocationUpdate(): Boolean {

       val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(ctx)

        val locationCallback = object : LocationCallback() {
            override fun onLocationResult(result: LocationResult?) {
                super.onLocationResult(result)
                result?.locations?.let { locations ->
                    for (location in locations) {
                        Log.d("MyWorker", "NEW Location: $location")
                    }
                }
            }
        }

        val locationRequest = LocationRequest().apply {
            interval = LOCATION_UPDATE_INTERVAL
            fastestInterval = LOCATION_FASTEST_UPDATE_INTERVAL
            priority = LocationRequest.PRIORITY_HIGH_ACCURACY
        }
        val result = fusedLocationProviderClient.requestLocationUpdates(
            locationRequest,
            locationCallback,
            Looper.getMainLooper()
        )
        Log.d("MyWorker", result.exception?.message.toString())
        Log.d("MyWorker", result.exception?.cause.toString())
        Log.d("MyWorker", result.exception.toString())
        return result.isSuccessful
    }

错误

I/WM-WorkerWrapper: Worker result FAILURE for Work [ id=3fe5b78d-e0ed-4348-90b6-25c2c1f96bfd, tags={ com.example.testapp.MyWorker } ]

Note: The Google Play store has updated its policy concerning device location, restricting background location access to apps that need it for their core functionality and meet related policy requirements. Adopting these best practices doesn't guarantee Google Play approves your app's usage of location in the background.

Learn more about the policy changes related to device location. https://support.google.com/googleplay/android-developer/answer/9799150