Service persist running in background and run at boot #279
Replies: 14 comments 5 replies
-
It's possible, however at the moment it seems unlikely to do it with Delphi using the "out-of-the-box" features, at least for later versions of Android. I've just revisited this idea in the last couple of days, without success, i.e. being able to have the service run without the application running (at least not reliably). Your scenario would be suited to using a Worker class however this is yet to be supported in Delphi. There was an enhancement request reported by me, here. |
Beta Was this translation helpful? Give feedback.
-
|
Delphi Works,
Thanks a lot for your answer, it was very enlightening, I must have spent 10 days trying to make the service run, using everything from Kastri and recommendations from the Internet and nothing worked. When you speak “out-of-the-box”, you refer to outside the embarcadero apis or out of what's available in Android's api without using “root”?
How does the tracking of the location work? There's a way to do in Kastri/delphi a service or app that captures the location of the device and sends it to a server or falls in the same case of service rolling, I'd like to keep my app running, but as plan B, could just capture the location, and at the shipping events of this data I send other things. Would that be possible, if so, what example can I use to ensure that the capture event and send the location to be executed? I read a lot about the schedule of an activity, which could from time to time be rounded by android, is that possible to do inside the delphi? Some example that this works?
Thank you very Much.
De: DelphiWorlds ***@***.***>
Data: domingo, 2 de março de 2025 às 14:53
Para: DelphiWorlds/Kastri ***@***.***>
Cc: Cristian Robert Gallas ***@***.***>, Author ***@***.***>
Assunto: Re: [DelphiWorlds/Kastri] Service persist running in background and run at boot (Discussion #279)
make the service run on independent background of the app running, that service stays active and initiate with the system when the debit is reset, is that possible to do on the android?
It's possible, however at the moment it seems unlikely to do it with Delphi using the "out-of-the-box" features, at least for later versions of Android. I've just revisited this idea in the last couple of days, without success, i.e. being able to have the service run without the application running (at least not reliably).
Your scenario would be suited to using a Worker class<https://developer.android.com/develop/background-work/background-tasks/persistent/getting-started#define_the_work> however this is yet to be supported in Delphi. There was an enhancement request reported by me, here<https://quality.embarcadero.com/browse/RSP-36884>.
—
Reply to this email directly, view it on GitHub<#279 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ADFWMGWXEGGJHZDXKY6Z6CD2SNOUDAVCNFSM6AAAAABYFDFW3KVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMZWHAYDCOI>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
By "out-of-the-box" I mean with what is included in Delphi itself.
This is an example of what you're referring to, and it is what I had been revisiting lately. Unfortunately as I say it has become difficult to have it work reliably when the app is not running. |
Beta Was this translation helpful? Give feedback.
-
|
This problem about embarcadero x android background service is because android api change in recent android updates? or it was never possible to do a persistent service in kastri/delphi? Do you think that there is some space for a hack or something component making embarcadero run in background or trigger app times to times, like any root cmd configuration?
To iOS, is possible use Kastri/Delphi to make a background service, or there is the same limitation?
Thank you,
De: DelphiWorlds ***@***.***>
Data: domingo, 2 de março de 2025 às 17:02
Para: DelphiWorlds/Kastri ***@***.***>
Cc: Cristian Robert Gallas ***@***.***>, Author ***@***.***>
Assunto: Re: [DelphiWorlds/Kastri] Service persist running in background and run at boot (Discussion #279)
When you speak “out-of-the-box”, you refer to outside the embarcadero apis or out of what's available in Android's api without using “root”?
By "out-of-the-box" I mean with what is included in Delphi itself.
How does the tracking of the location work? There's a way to do in Kastri/delphi a service or app that captures the location of the device and sends it to a server
This is an example<https://github.com/DelphiWorlds/Kastri/tree/master/Demos/CrossPlatformLocation> of what you're referring to, and it is what I had been revisiting lately. Unfortunately as I say it has become difficult to have it work reliably when the app is not running.
—
Reply to this email directly, view it on GitHub<#279 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ADFWMGX3KJQQCO42VKCGOAD2SN5XXAVCNFSM6AAAAABYFDFW3KVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMZWHA3DSMA>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Yes, the newer versions of Android have made it difficult to do services like that in the same fashion that worked in earlier versions.
It is possible to perform certain functions in the background on iOS, such as location updates. As newer iOS versions have come out, they have also introduced more restrictions, however it is still possible to reliably monitor location updates - they are just more limited (so everyone regardless of which dev tool they use, is affected). |
Beta Was this translation helpful? Give feedback.
-
Are you compiling the service code into your application? Usually Java source will be compiled into a .jar file, and then added to the project, like I do with a number of demos in Kastri. Regardless, this approach does not work for later versions of Android - this is nothing to do with Delphi, but because of restrictions in Android.
I expect that for what you need to do, using the Worker class (or more precisely, making a descendant of it for use with WorkManager) may be the best solution however as I said earlier, it's yet to be supported in Delphi. |
Beta Was this translation helpful? Give feedback.
-
|
I used the example with Delphi 12.0, and when I close the app, the service also stops. Do you think updating to Delphi 12.3 would make it work even after the app is closed? |
Beta Was this translation helpful? Give feedback.
-
Which demo are you referring to? If it is the Android location demo in the Playground repository as mentioned in my last comment, the service is not intended to keep running, since it runs only when a location update is available. If it is something else, please indicate which. I doubt that upgrading to Delphi 12.3 would change anything for it |
Beta Was this translation helpful? Give feedback.
-
|
Sorry, my English is not very good. I referred to this https://github.com/DelphiWorlds/Kastri/tree/master/Demos/CrossPlatformLocation When I close the app, the service stops as well. I took the latest version from GitHub to try (to test). I made some modifications, but it still closes. i will download 12.3 to try. |
Beta Was this translation helpful? Give feedback.
-
Yes, it's quite likely that will happen with that demo on newer versions of Android. If you're looking for an Android only solution, I suggest using the other demo I mentioned. Just remember that the service runs only when a location update becomes available. |
Beta Was this translation helpful? Give feedback.
-
|
I will take a look at the example you shared. In the other one, I had created a threading to run some tasks in the service in addition to the location part. Thank you for being so helpful. Best regards. |
Beta Was this translation helpful? Give feedback.
-
What kind of tasks, exactly? I'm considering doing something about this, here. |
Beta Was this translation helpful? Give feedback.
-
|
I disabled the location access at the start when the app runs to protect user privacy. I created a URL that indicates whether an alert should be triggered on the phone. It's for gas leak alarms on workers’ phones. So I made a thread that checks the URL every 5 seconds. If an alarm should be triggered, it raises the phone’s volume to maximum, plays a very annoying alarm WAV file, and then retrieves the users’ locations and shows them on the website that triggered the alarm—just in case someone is in danger. Everything is working great, except that if I swipe the app away to close it, everything stops working. The app shuts down and so does the service (which should have kept running). I’m going to take a look at the one you sent—AndroidBackgroundLocation—to see if I can change something and prevent the app from killing the service when it's closed. I’ll also update my Delphi to 12.3 since it’s about time. I couldn’t get it to start on boot either when the phone restarts, but I found an app that does it for me (in Portuguese it's called "Gestor de Inicialização") on the Play Store. However, I plan to implement a monitoring system to check if the service isn’t running, and then send a WhatsApp message or a push notification to the person so they can click and open the app, starting the service manually if the boot fails. I have these alternatives in place to make sure everything works properly. Sorry by the large text :D |
Beta Was this translation helpful? Give feedback.
-
Does this really need to occur that often? Android are really clamping down on code that executes in the background, since battery life can suffer as a result.
This is most likely due to restrictions in newer versions of Android, where services need to specify what they are doing in the when they run in the "foreground". When the service starts in the foreground, Android checks if this value exists, and if it does not, shuts down the service. Currently, Delphi does not have support for this attribute, so the manifest needs to be changed manually to allow for it.
In your case, this is unlikely to help, as the demo is not intended to keep the service running - it is started whenever a location update becomes available (or when it receives an "instruction" from the app), and terminates once the action has been dealt with. Having said that, changing the OnServiceCommand result to
If you have the infrastructure to send push notifications, that would be a much better solution than having the app itself monitor a URL.
No problem! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to build an application that runs on the Boot of device, and it's running around in background, I need to connect from time to time on a cloud server and send data, I've tried to use all the examples of Kastri that have anything to do with it, AndroidServiceComm, AndroidStartAtBoot, but I can't make it work, I'm wondering if it's viable to make an app like that on the Android. Any can tell me how to take the example AndroidServiceComm and make the service run on independent background of the app running, that service stays active and initiate with the system when the debit is reset, is that possible to do on the android?
Beta Was this translation helpful? Give feedback.
All reactions