Minimalistic Android Service with Delphi 10 Seattle

I always love making minimalistic demos because then you can see all the essential parts. I put together this short video with Delphi 10 Seattle to show the minimal parts necessary for to create an Android service. It is so simple. The Seattle release supports 4 different types of Android services. One thing this demo does show that is optional, is it create a sticky service that will relaunch if pushed out of memory. It doesn’t show how to talk to methods on the service – there are a few ways, which I can cover later. [Source]

BTW, Delphi 10 Seattle added support for iOS background execution too.

43 replies on “Minimalistic Android Service with Delphi 10 Seattle”

Do you have a video describing how your development environment is setup for debugging? Is that a Nexus 5 simulator or connection to real hardware?

Excellent quick demo!

Would be useful to have another video with bit more background (no pun intended) on how to work with the background service..

I assume backround service runs on the context of the main thread (normal Android behavior) so for any meaningful work one still needs to have a worker thread created in the service.

Jim, Every time I add a unit in service, Delphi goes on to give the following error Exception Exception class with message ‘Activity not found, maybe you are in the service.’It can not add units in a service?

Thank you;

Saulo Benvenutti

Very good this example, as it put a timer, and a notification for every 10 seconds to make a notification but does not work you tell me why that?
Thank you.

I also get ‘Activity not found, maybe you are in the service’. What that error message means?

Hi Jim,

Thanks for the demo – it’s great to have some examples of the various features.

Would it be possible to also give a demo of the service doing something – maybe a notification or a timer (or both???)

Thanks for your great articles!

Hi, I need to add a trestclient compoment in a service, but the delphi to give the exception: Activity not found, maybe you are in the service.
What can I do to fix it?

Thank you

Marcio: Are you trying to run the service? You need to run the Host app.

Saulo: I’m not able to reproduce your error and am not sure what steps you did to get it. Are you running the service instead of the app?

Jim, all the services I’ve tried to do, as this equal to mimimalístico works, ie the service goes normal (although do nothing), but if I create a simple thread memo without nunhum the Delphi code begins to generate exceptions. Debugging, I realized that there is a time that it tries to capture the atidade service, and always generates the same error “Activity not found, maybe you are in the service.” identical to the error that Márcio are having, to obtain the best I can send the sources and the apk so you can locate the error, just need to know where to send!

Thanks, …

I just realized you may be trying to modify the user interface from the Service. That is a big no-no and the error you are getting is probably from the Android OS. Remember, the service is running in an entirely different process. It does not have access to the Activity at all. You can’t even make Toast from a service.

Sorry Jim, What do I need to do to check a webservice, from android service, every 10 minutes?

I have also been receiving ‘Activity not found, maybe you are in the service.’ messages. This happens as soon as I make any change to the service code, even something simple (not UI). The “add android service” should be accompanied by “refresh android service” and “delete android service” so that we can be sure that the latest changes to the service code are compiled into the host. There needs to be DOCUMENTATION about what is allowed in a service and what is not, please!

If I close my application => its background service is closed too. How to make service that’ll continue working all time and start automatically after restarting the phone?

Can the service to work with components LocationSensor, KinveyProvider, BackendStorage? I tried to save simple data in the table on Kinvey server, but without success – no error, no result.

Turns out a number of the components are referencing the Android Activity, which leads to the “Activity not found, maybe you are in the service” errors. The solution is to use a lower level API call, or some of these will be fixed in update 1.

@Teras: Make your service sticky like I showed in the video. That will keep the service running. The LocationSensor doesn’t work yet, but check out the skill sprint I just did where I show how to access the location API and post to Firebase with the HTTP components.

@MARCIO: To check a web service every 10 minutes you would need to make a thread or task that sleeps for 10 minutes, and then wakes up and checks the web service. See the skill sprint video I just posted.

jim, I followed your example but using tcpclient to send simple text message to a tcpserver…
it always gives me : TidTcpClient CLASS NOT FOUND in the service unit … why ???

I’ve been working with service programming for a month now, and I can advise you not to use any high-level components in the service. Jim recommended TNetHttpClient and TNetHttpRequest, which do work in a service. Try to use those for your messaging instead of the Indy components.

I can confirm what Haltech said. I use Android 5.0.2 and the service not works. 🙁 Does anyone have an idea?

Hello Friends, Thank you for you demo
I’ve tested the app but I get the next error

“unafortunately RADService has stopped”
Any Idea how I can fix it?

Thank you.

Look at the host project deployment. There’s a hardcoded c:\users\jim… path that must be updated to work on your computer.

hello, thank you Jim by you post.
I get the same error like peter, “unafortunately RADService has stopped”.
NolaSpeaker there aren’t harcoded path into my deployment screen maybe the source code was updated by Jim.

The project compile fine, it crash in runtime loading the app.


There IS a deployment issue. In the deployment manager for RADHost, it says ‘C:\Users\Jim\Documents\Embarcadero\Studio\Projects\Android\Debug”. Now obviously if your deployment is bad, you will not be able to run the application.

I’ve solved my problem uninstalling all version of Delphi in my computer and a version of Andorid Studio and install delphi 10 seattle again.
the demo work fine now.

Thank you

Solved!! Thank you…

Another question..
reading the code I can see that the service is started when the Form is created, so , mi question is ..
Is possible to build a service that start automatically, ex: when the phone start ?


Yes it is. I have done it. And it’s a lot of very tricky stuff to get through. You have to edit the android manifest and merge the classes.dex to create a boot listener!? Lookup Dany Wind’s article. Read up on Brian Long. Ask a question on Stack Overflow, and I’ll try to answer it there.

Hi, i download this source code of the video and i can to run this. I see the Run option disable. I follow step by step the video and when i add the service to the project, the Run option is disabled. What i can to do for to fix this or test this sample? i have delphi 10.1 Berlin trial.

I have a START_STICKY service just as shown in the video. (delphi tokyo 10.2) I started a task in the service’s onstartcommand, but it stops running as soon as application is closed.

when I add theses units in service data module it doesn’t work.

REST.Backend.PushTypes, System.JSON, REST.Backend.KinveyPushDevice,
System.PushNotification, FMX.StdCtrls, FMX.Controls.Presentation,
Data.Bind.Components, Data.Bind.ObjectScope, REST.Backend.BindSource,
REST.Backend.PushDevice, REST.Backend.KinveyProvider

Comments are closed.