Wednesday, 6 November 2013

Delphi and Android services

Since Delphi’s introduction of Android support with the release of XE5, there have been many questions posted hither and thither regarding whether and how you can build service applications with it. The pervading suggestion or suspicion is that maybe Delphi isn’t cut out to build such exotic Android creations.

Well, if your goal is to just build some sort of a service app I wouldn’t particularly recommend Delphi for the job right now, simply because it hasn’t got the wherewithal to support you in that endeavour in its current guise, and consequently it’s quite difficult. Really, if you just want to build a service app you’d probably have an easier time using Java or Oxygene for Java in conjunction with the Android SDK.

However if you are invested in building a Delphi Android app, are in the midst of building a FireMonkey application for mobile deployment, and need an Android service as part of that, well it’s quite possible. But as I may have mentioned, it’s neither particularly easy nor especially straightforward.

I am aware that I have (at this time of writing) yet to write up the business of adding Android menus to Delphi apps, employing a splash screen and launching Android activities and getting results back (as demo’d in my CodeRage 8 talk, albeit with code samples available in that linked post)*.

Now I can add to the list of things I have yet to write up the steps for creating a service application. If you take a look at this Google Play Store listing you’ll see I’ve built an app that, as well as a splash screen, also implements a service.

Service app running Service app includes a service

So yes, it’s quite feasible to build a service and a broadcast receiver in a Delphi app. That said, I’ve had to be a little creative on exiting the app to avoid a failure being evident on the screen.

As mentioned, these Android aspects that fall outside the standard simple business app paradigm haven’t factored into what is readily supported currently in Delphi’s Android support. Hopefully this will be improved as time goes on to allow all sorts of Android features and facets to be surfaced in Delphi Android code. In the mean time a certain degree of spelunking and skulduggery is needed to achieve certain ends.

I know quite a few people are clamouring for knowledge on how to build service apps. Hopefully in the near future I’ll write it up, but certainly in the nearer future I’ll release the code sample, once I’ve exhausted possibilities to clean up the wrinkle around app shutdown. That should satisfy the keener of you out there. In the mean time, be satisfied that it’s been proved to be possible at all.

42 comments:

  1. On my phone (Xiaomi M2A), the demo app disappears immediately after showing the splash screen. But perhaps that’s due to memory problems.

    On my tablet (Galaxy Note 10.1), the demo app works fine, happily toasting away.
    The splash screen does cause the display to rotate to portrait mode; and even though it rotates back as soon as the app’s "main form" shows up, it looks rather ...restless.

    ReplyDelete
    Replies
    1. Thanks for the feedback, Martijn.
      Interesting about the app dying - can you run up Monitor to see the logcat and see why it is dying? I assume the M2A has ARMv 7 and NEON?
      Also, re the splash screen, I deliberately forced it to portrait as I *thought* splash screens typically had a fixed orientation. Maybe I need to look at Angry Birds again ;o) I assume you'd suggest either both splash screen & form rotates according to the device, or neither does?

      Delete
    2. The M2A runs my own XE5 app without problem, so ARMv7 and NEON are not the problem.
      But as I said, using the default Android skin (MIUI), it’s got very little memory left by default (about 184M after cleaning up the memory), so perhaps it needs to kill the app, in order to have enough memory to launch the service?

      I’ll see what I can find in the logcat. What ‘Monitor’ are you referring to?

      Re the splash screen: I much, much, prefer when apps just keep running in the current orientation. Since the main form does that just fine, it’s a tad disturbing that it turns around _just for a splash screen_.
      In the end, the splash screen should behave consistently with the rest of the app: if the app is locked to a fixed orientation, then the splash screen should be fixed to that same orientation. But if the app works fine in any orientation, then so should the splash screen.

      (It’s especially irritating on devices with no hardware buttons, like the Galaxy Note: whenever an app takes some time to load, I have a tendency to quickly pop up the notifications drawer and see if anything interesting’s come up in the meantime — or check the battery status, etc. If the orientation switches at that moment, the notifications drawer changes place as well, and I can’t carry on doing what I wanted to do. By the time I’ve mentally reassessed the orientation and am reaching for the new location, the app has finished loading, and the orientation will switch back, again frustrating my intentions. Twice frustrated just while starting up an app does not bode well for future usage :-P).

      Delete
    3. Duly noted re your RAM situation.
      Monitor is a tool in the Android SDK directory tree - under build-tools I think, from memory - that launches a GUI to show a filterable view on logcat - much more manageable than using logcat via adb from a command prompt.
      I shall take note of your well articulated annoyance at splash screens that fix their orientation :o) I'll try and sort an update to it at some point soon. It will be good to have that sorted for when I get round to writing up about splash screens!

      Delete
    4. Ah, yes. A filterable GUI is indeed handier than dumping the logcat and then filtering it using Notepad++... :-)

      But anyway, the problem appears to occur when attempting to create an external files directory:

      D/Splash (31863): Supported OS version
      D/Splash (31863): ARMv7 CPU detected
      D/Splash (31863): NEON instructions supported
      I/ActivityManager( 559): Displayed com.embarcadero.ServiceApp/com.blong.test.SplashActivity: +733ms
      D/Splash (31863): Launching the main activity now
      I/ActivityManager( 559): START {cmp=com.embarcadero.ServiceApp/com.embarcadero.firemonkey.FMXNativeActivity u=0} from pid 31863
      W/ApplicationContext(31863): Unable to create external files directory
      I/WindowState( 559): WIN DEATH: Window{422d7f58 com.embarcadero.ServiceApp/com.blong.test.SplashActivity paused=false}
      I/ActivityManager( 559): Process com.embarcadero.ServiceApp (pid 31863) has died.

      Delete
    5. Oh, and there’s the following bits as well:

      W/InputDispatcher( 559): channel '42902720 com.embarcadero.ServiceApp/com.embarcadero.firemonkey.FMXNativeActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
      E/InputDispatcher( 559): channel '42902720 com.embarcadero.ServiceApp/com.embarcadero.firemonkey.FMXNativeActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
      W/InputDispatcher( 559): Attempted to unregister already unregistered input channel '42902720 com.embarcadero.ServiceApp/com.embarcadero.firemonkey.FMXNativeActivity (server)'
      I/WindowState( 559): WIN DEATH: Window{42902720 com.embarcadero.ServiceApp/com.embarcadero.firemonkey.FMXNativeActivity paused=false}
      I/ActivityManager( 559): Displayed com.embarcadero.ServiceApp/com.embarcadero.firemonkey.FMXNativeActivity: +883ms

      Delete
    6. Lol. I took off the Write External Files permission.
      I'd forgotten that some devices *really* want to write to external storage, whereas others do not (or is it some versions of Android - can't remember).
      Thanks for the investigation. I'll add the permission back in and re-upload tonight.

      Delete
    7. Heh, yes. I’ve noticed that the M2A has a tendency to be quite restrictive in the permissions department — don’t know if that stems from Android 4.1, or MIUI, or something else.

      Anyhow, thanks a *lot*, both for you investigative efforts and sharing your findings!
      I’ll be very interested to see the resulting blog post(s).

      Delete
    8. FYI: the updated app works fine on the M2A. And thank you very much indeed for making the splash screen respect the current orientation! :-D

      Delete
    9. Hi Bryan,
      Do you have a example without the portrait restriction?
      When I remove this in the manifest file it's works on my Phone but on a larger device the screen get's mixed up after rotation.

      Delete
    10. Odd. My understanding was that v1.0.4 I uploaded some while back had the portrait restriction removed (see the What's New info on the listing). Can you confirm you have the current version installed?

      Delete
    11. Thanks for you're response But I downloaded the code from http://blong.com/Downloads/AccessingTheAndroidAPI.7z which includes the SplashscreenTest project.
      The manifest file is forcing the app in portrait.
      Also I can not find any "what's new" in the listing.

      I guess I'm working with a different version

      Delete
    12. Oh, I see. The online sample app, to which this post mostly refers, has the portrait restriction removed. I think I just edited the manifest, from memory (I don't have the sample to hand just now).
      Can you install the service app from Google Play and see if that suffers the same screen issue to which you allude? And can you be more specific about how and exactly when this screen mess-up occurs?

      Delete
    13. Yes same issue there.
      When you start the app you first notice that there is less space between the start service button and the upper screen border. In the service this is not an issue, but in my app the title of the app is only partly visible.
      Then when you rotate the device the screen keeps the same horizontal width, leaving a white space to right.
      If you keep rotating sometimes even the start and stop buttons are not visible.
      If you want I can start making some screenshots and send them to you.

      Delete
    14. Oh, ok, yes I've seen that issue on some apps. Not with this one though. Actually, I'm trying to think if it was with more than 1 app.
      Unfortunately I never worked out what caused the problem with the app. I did manage to get it gone though, by a tedious process of rebuilding the UI in a different project. There was something up with, oh I dunno, the content of the .fmx file or something.
      It may be worth your while creating another form in the project, mimicking what is in the first one, then deleting the first one and renaming the new one to be in the same unit and have the same form name as the first. You may get lucky as I did.

      Delete
  2. oh nice! i was waiting this app a lot! I will wait for the example code! on Nexus 4 all worked perfectly

    ReplyDelete
    Replies
    1. Please refer to http://blog.blong.com/2013/11/delphi-and-android-services-part-2.html

      Delete
  3. can u share ur code or can u explain for us budy? still no more documents on the net about delphi on mobile platforms. we should start from something. greetings

    ReplyDelete
    Replies
    1. Yes, as mentioned in the post, I'll post the sample soon when I'm sure I can't do anything more with the problem I currently have with it.
      At some point I'll also write up some of these more exotic Android areas from a Delphi perspective, but tat won't be till later. Too little time just now.
      All will be forthcoming though.

      Delete
    2. Please refer to http://blog.blong.com/2013/11/delphi-and-android-services-part-2.html

      Delete
  4. Hi, i m not find source for service app.
    Thanks

    ReplyDelete
    Replies
    1. Yes, that's because I haven't made it available yet. I thought that was reasonably clear from the text in the post and in replies to some comments.

      Delete
    2. I download and try your sample on Samsung Galaxy Note II (N7100).
      i can not see any problem.
      With the current state can be useful for us.
      Thanks for your helpful information on all subjects.

      Delete
    3. Look at the logcat when you exit the app.
      I've "hidden" the problem from the UI, but the exiting is a mess...
      Am still looking at whether it is feasible to clean that up at all as Delphi currently stands.

      Delete
    4. Opps. i see :)
      Thanks for your information.
      i wait for solution. because i m beginner for android.
      i need to xe5
      greetings

      Delete
    5. Please refer to http://blog.blong.com/2013/11/delphi-and-android-services-part-2.html - however the aforementioned flaw is still present in the sample. I haven't yet sorted the problem out.

      Delete
  5. did u build console app for service app? maybe its work and u dont need to hide gui !?!

    ReplyDelete
    Replies
    1. A service is a component of your Android app, and typically you'll have a UI surfacing the service as appropriate. So no, there's no real notion of a console for the service app. The service is a class and the app is implemented as an activity, which is another class. Though in the case of Delphi that activity is implemented by all your Delphi forms.

      Delete
  6. In your service did you start delphi code or java code?

    ReplyDelete
    Replies
    1. It's a Java entry point, as is required, which then calls into Delphi code.

      Delete
  7. I have to download and install the Demo sevice App,This Demo program so cool,
    But no sorce code can provide a reference :(

    ReplyDelete
    Replies
    1. Please refer to http://blog.blong.com/2013/11/delphi-and-android-services-part-2.html

      Delete
  8. is this example ready?

    ReplyDelete
    Replies
    1. Yes, please refer to http://blog.blong.com/2013/11/delphi-and-android-services-part-2.html

      Delete
  9. Before selling xe5 should have discussed what you can not do (I would not have purchased had known that something as important as creating a service and would have used more time to know android directly).
    Create a service is the most important things in android.

    I really also like to know how to solve this as your example.
    Congratulations on being THE ONE who has deigned to do so.
    (We'll be waiting for your wisdom and your code)

    ReplyDelete
    Replies
    1. Thanks! I'm not sure I'd concur that services are the *most* important thing, but thy are quite commonly required in many Android apps.
      My *almost* finished sample is at Please refer to http://blog.blong.com/2013/11/delphi-and-android-services-part-2.html

      Delete
  10. Confirm that downloaded demo from google play store works well. Splash screen works on horizontal orientation without forcing screen to vertical.

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. Replies
    1. DO you mean that app on Google Play isn't working, or the sample code - see update at end of http://blog.blong.com/2015/02/delphi-and-android-services-part-3.html - isn't working with Android 6?

      Delete