Android Background and Foreground location services

91 Views Asked by At

Great People out there! I am writing the code for the location tracking I have written both foreground service and a coroutine worker for the background location update. But the problem is when I use the foreground service the user grants all the time permission. Foreground Service started recording the location for the background as well. I want both in their respective paradigm like foreground service only until the app is foreground and the background worker monitor location in the background. I have read all the documents I don't get what I am doing wrong or is this the behaviour of the services?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        ContextCompat.startForegroundService(fragment.requireContext(), serviceIntent)
    } else {
        fragment.requireContext().startService(serviceIntent)
    }

foreground service using fusedlocation and background location service using coroutine worker to create a background worker thread.

 WorkManager.getInstance(requireContext()).enqueueUniquePeriodicWork(
            BackgroundService.workName,
            ExistingPeriodicWorkPolicy.KEEP,
            PeriodicWorkRequestBuilder<BackgroundService>(
                15,
                TimeUnit.MINUTES,
            ).build(),
        )
0

There are 0 best solutions below