Android Tutorial Creating a sticky service - long running background tasks

Edit: Things have changed with newer versions of Android and new restrictions. Sticky services should not be used any more.

Background service example: https://www.b4x.com/android/forum/threads/background-location-tracking.99873/#content

This tutorial is not 100% correct anymore.

There are several ways to handle background tasks.
1. You can use StartServiceAt to schedule a service to run at a specific time. When the service is started you can schedule the next one.

This way you can create a service that runs every x minutes or hours. This option is good for all kinds of updates. The service can connect to your server and check whether there are any new messages for example. (Note that for "realtime" notifications you should use the Push framework)

2. If you have a long running task and you don't want the OS to kill your process when there is no visible activity then you should call Service.StartForeground. Android will treat your application as if it is a visible application and will not kill it.

However an ongoing notification icon will be added to notify the user that your application is actively running. A music player for example should use this option.

3. The third option which is supported through the attributes modules feature is to mark your service as a sticky service.

This is done by adding the following attribute to the service code:
B4X:
#StartCommandReturnValue: android.app.Service.START_STICKY

A sticky service is somewhere between a regular service and a foreground service. Android will kill the process from time to time. However it will recreate it automatically (as soon as possible). For example if you want to track the device location you can use this option. The service (together with the process) will be killed however it will also be recreated again.

Related tutorial: The result of swiping an app from the recent apps list
 
Last edited:

RichyK68

Active Member
Licensed User
Longtime User
They do work, but they get killed when memory gets low and can never restart from what I can tell due to Lollipop's shockingly ruthless memory management (unless the user manually re-opens the app).

Is there a workaround? I want my foreground service to restart if it gets killed. Else it defeats the point of the app. And just makes Android useless in many ways for a developer when Google make these horrible decisions.

Richy
 

Daniel-White

Active Member
Licensed User
Longtime User
Among "StartServiceAT" , Service.StartForeground and Sticky service.

Which one save more life time battery, ?, an example for GPS tracking APP (take only GPS data each 2 minutes), and Why is the best approach.?

Thanks you so much
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
an example for GPS tracking APP
Hello,
In that case I would say : PhoneWakeState and FusedLocationProvider because the data will be available immediately.
But if you don't care waiting, StartServiceAtExact
 

Daniel-White

Active Member
Licensed User
Longtime User
Thanks Lemonisdead. I am actually using FusedLocation lib, and work great, and using StartServiceAT to schedule it to work each 2 minutes. However I am all ear to any other approach . Or I am in the correct path. :)
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
StartServiceAT to schedule it to work each 2 minutes.
It is a good starting point but I wonder if you wouldn't have less energy consumption by acquiring a partial lock and letting FusedLocation check every two minutes. Just a thought because it seems (for me, for my understanding) to be a heavier process to start a service each two minutes (short delay). But it should be interesting to prototype both strategies and compare the results :)
 

cxdzbl

Active Member
Licensed User
Recently, I've read about service on the Internet, and now it's hard to keep it running in the background. As the Android version improves, all sorts of methods fail. My version of the Samsung S7, Android 7, recently upgraded after the discovery, I think in the Service_Destroy to run another service to listen to each other seems to have no reaction, close the application in the list of recent operation, service was killed and the Service_Destroy code is not executed. I wonder if b4a has an effective way to make service live a little more?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
You are posting at the wrong place. You alread have created a thread for this or not?
No need to put this comment to all threads you can find!
 
Top