I am working on tracking app where i get the location update in background mode from gps.
I have search on google i found that Android 12 - Foreground service launch restrictions.
So you have use Work manager insted of foreground intentService.
And workManager minimum limit is 15 minutues for WorkManager Periodicity. Then how can i get location update every seconds in background mode.
Because workmanager request not run every seconds.
Thank you.
When you are actively tracking the user you should use a Foreground Service. WorkManager is for cases where you care for the Work to finish, but the priority will be to save the battery.
I can assure you that WorkManager is the wrong API for you.
Why do you need to start the service from the background? What is your exact use case?
Check here:
https://developer.android.com/guide/background#replacing-foregound
Some use cases for using foreground services directly are as follows:
Media playback
Activity tracking
Location sharing
Voice or video calls
Related
Let's say I want to build an app which requests current location periodically (e.g., every 10 minutes, this number should be configurable) and submits to a server.
I'm aware that Foreground Service and WorkManager are normally suggested for this kind of scenario. However which is would suit more? Below are my thoughts and doubts.
WorkManager - is mainly for deferrable background work whose execution is guaranteed. However I know that from Android 8 (API 26) background location was introduced and that restrict location to be updated only a few times every hour https://developer.android.com/about/versions/oreo/background-location-limits. Thus this perhaps doesn't meet the periodical updates as per the requirement.
ForegroundService - is perfect for something that runs and needs to make users aware of. It's recommended for this kinda scenario (location tracking) for privacy purpose. Google also creates a sample app to promote this practice https://github.com/android/location-samples/tree/master/LocationUpdatesForegroundService.
From the above analysis, it seems ForegroundService is the one. However I also found that WorkManager has a built-in support to use Worker in conjunction with ForegroundService via androidx.work.impl.foreground.SystemForegroundService https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running#long-running-kotlin
That makes me confused as to what should I use and what Google really recommend for this specific scenario.
Anyone has any idea?
If you want to communicate somehow with the service then use foreground service and if you want to have some processed input based on something else you did in that work manager then choose work manager.
Work manager doesn't have option to redeliver intents and all other commands like start sticky etc...
Since work manager is more suitable for syncing data with db, processing a file etc..
If you were to ask me, I'd choose foreground service since you can add a type location to the xml tag when you register it in the manifest.
Both of these solutions don't survive OEMs aggressive battery restrictions since WorkManager's work can be deferred and if I want instant execution combined with wake locks I can easily do it in the foreground service since it also has a binder option that works well for UI sync.
Can anyone please explain to me at technical level, why does a music player's service run continuously after the screen is turned off. Why is it never affected by doze mode? I want to create a service along similar lines, but it should perform a particular task every 20 seconds continuously.
I would be glad if anyone can explain this. Thanks in advance.
The music player's service runs as a 'foreground service'.
Foreground services for the most part are unaffected by doze when used in combination with a partial wakelock:
Purpose of WakeLock in modern Android?
If your app needs to create a foreground service while the app is in the background:
ContextCompat.startForegroundService()
Inside the service, promote it to the foreground and post the notification by calling:
startForeground()
From the Android docs:
"A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app."
https://developer.android.com/guide/components/services
Typically, a music app will change the foreground service notification from a 'liability' that annoys the user to an asset by displaying an image of the artist, the current track and offer media buttons like play/pause/rewind/forward.
A foreground service should not generally be used to do regularly scheduled work because it will greatly reduce the device's battery life.
Instead, try to use WorkManager, JobScheduler or Firebase Cloud Messaging to wake the device up only when necessary.
Execute frequent tasks using the Work Manager API
But it will not be possible to "poll" or run your code every 20 seconds with these solutions. You really need to find a solution that works with "Doze" instead of fighting against it.
Have been trying to use fused location, and location services in order to get a location in the background and when the app is not running.
Unfortunately, everything worked fine until android 8 came.
I have an app that needs to send the location to a server at least every minute when the app is not in the background and when not running.
Fused location is not giving the location as I set in the properties.
Thanks.
So, in android 8 and above, the best way to get the location in the background, or when the app is not running, would be using the Alarm Manager. The minimum periodic time alarm can be turned on is 60 sec, this is the best way with the minimum time to update the server with the location.
I recommend using JobScheduler.
You can reschedule your Job Service to do its Job every minute.
I need to send what businesses the user is currently at every 5 minutes. I am using the Google Places library and can successfully grab the businesses. The problem is this service needs to be going 24/7.
My solution so far is to use a service called from my main activity. This service will subscribe to background location updates every 5 minutes. Once the location update comes through, it will ask the Google Place library for the businesses the user is currently at. I am using the Google Play Services to subscribe to these location updates. I read that using an Alarm for something like this isn't recommended, that is why I am subscribing to location updates to give me a pseudo setTimeout(5mins) behavior. I am actually not even using the GPS returned, just the function callback to ask GooglePlaces.
The problem is that this service is unbelievably unreliable. I am running Android 8.0 with 11.2.0 play services installed. The service's OnDestroy is called within a few minutes and is never restarted even though I return Start_sticky from the service OnStartCommand. Are services supposed to get killed this quickly? I have no other apps open and the service is 99% of the time just idle waiting for the location tick to come through.
Is there a better way to do this? All I really need is a function called every 5 minutes so I can ask Google Place where the user is at. It doesn't even have to be location related. The only requirements are it runs even when the app is closed.
Try to use AlarmManager with service... try this once maybe it will solve your problem.
Thanks!!!
My solution was to use AlarmManager started from my Activity. No Services needed.
I'm using activity recognition api and location using google play services and I'm aware of how battery consuming they are so I would actually be pleased if this is the case and the Job Service stops when the screen is on which limits the collection of data to when the user is actually using his phone and not 24/7.
When I tried testing this, sometimes it stops after half an hour, sometimes more and at times it happens immediately so is this related to other background services and the current apps that are being used ? and is there a way to force the service to stop when the screen is off ?
As for the code I'm using the exact sample that I downloaded from the repository and the only modification that I made was calling the recognition service from the job service whenever the service is triggered (periodic task)
If I am understanding correctly, you have a JobDispatcher which repeatedly schedules the job to run after a fixed amount of time. If that's the case, then you do have the option to set the job to only run when the device is in idle or charging state.
This behaviour can be set with the JobInfo object that is passed to the JobScheduler.schedule(JobInfo job) method.
You can look at the documentation for JobInfo for refrence: https://developer.android.com/reference/android/app/job/JobInfo.html