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.

This entry was posted in Android and tagged . Bookmark the permalink.

40 Responses to Minimalistic Android Service with Delphi 10 Seattle

  1. Mark says:

    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?

  2. Bill says:

    Thank you. 🙂
    I love minimal demos too.

  3. Jim McKeeth says:

    This video is basically how I am setup. The nexus 5 is actually hardware. http://youtu.be/_OmWPVOfPnI

  4. Mark says:

    Excellent. Thanks!

  5. Claudio Piffer says:

    Thank you very much Jim. Very Usefull!

  6. Have you try to do something with your service ?
    Me, I was enabled to push a notification or something else …

    Perhaps the was too minimal 🙁

  7. pie.o.my says:

    Thank you for share.

  8. Ajutine Ai says:

    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.

  9. Saulo Benvenutti says:

    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

  10. Robert says:

    Hello,
    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.

  11. Jukka says:

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

  12. Peter P says:

    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!

  13. Haltech says:

    service not working on android lollipop 5.0 but working on android 4.4 and 5.1

    I don’t know cause.

    What happened ?????

    thank,

  14. Jim McKeeth says:

    Check out this other service blog post. It shows notifications and a local service:

    https://community.embarcadero.com/blogs/entry/download-an-image-in-background-with-android-services

  15. Jim McKeeth says:

    Also, I’ll go into more detail on Android Services during the Deep Dive on Saturday. Even if you can’t make it register for the replay http://forms.embarcadero.com/RAD10SeattleDeepDive?cid=701G0000000tKU2

  16. Saulo Benvenutti says:

    Please Jim … i need help

  17. MARCIO TENREIRO says:

    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

  18. Jim McKeeth says:

    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?

  19. Saulo Benvenutti says:

    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, …

  20. Jim McKeeth says:

    You can post a link here, or email it to jim@ this domain.

  21. Jim McKeeth says:

    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.

  22. MARCIO TENREIRO says:

    Hi Jim. this is the link: https://www.unimar.br/app/AndroidNotificationServiceDemo.rar
    run with debug mode. You are gonna see the error.

  23. MARCIO TENREIRO says:

    No Jim, I’m running de app host. look the url..

  24. MARCIO TENREIRO says:

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

  25. NolaSpeaker on SF says:

    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!

  26. Taras says:

    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.

  27. Jim McKeeth says:

    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. http://embt.co/sprint-android-services

    @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.

  28. Antonio Hanna says:

    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 ???

  29. nolaspeaker says:

    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.

  30. Arpad says:

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

  31. Peter says:

    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.

  32. NolaSpeaker on SF says:

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

  33. RLUNA says:

    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.

    regards.

  34. NolaSpeaker says:

    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.

  35. Peter says:

    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

  36. RLuna says:

    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 ?

    regards..

  37. NolaSpeaker says:

    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.

  38. Edgar Mejia says:

    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.

  39. Edgar Mejia says:

    How i can to download the source code of this page http://embt.co/sprint-android-services of skill sprint? i im trying of the link of this page: http://cc.embarcadero.com/item/30439 but i get this error: Server Error in ‘/’ Application.

  40. Jim McKeeth says:

    I was able to download it fine just now. Try a different browser maybe.

Leave a Reply