Friday 9 September 2016

Android callbacks wrapped by FireMonkey

A couple of years ago (exactly!) I posted on this blog about an article I’d written on accessing NFC tags from Delphi Android applications. At the time this was quite an involved exercise, requiring custom Java code to be compiled and linked in to the application in order to respond to the pertinent Android callback, Activity.onNewIntent. Indeed, Delphi versions XE5, XE6 and XE7 all have slightly differing capabilities, with each new release making certain Android operations rather easier.

While catching up on what’s new in recent releases of Delphi I realised a change had been snuck into Delphi 10 Seattle that makes the whole NFC exercise much more straightforward now in Delphi.

What this change means is:

  • No more custom Java code required!
  • No more linking in a custom Java library file
  • No more activity custom subclassing!
  • No more debugging obstacles!

This change seems to have gone in completely under the radar*, escaping the attention of the documentation department, so I thought it best that I brought it out into the open.

It turns out that the key activity callback that NFC foreground dispatch relies upon (Activity.onNewIntent) is now hookable from Delphi code (with the right know-how) and so all the Java shenanigans can be put behind us (at least in the context of NFC access). This callback is used in Android in all sorts of other scenarios as well, so if you find a requirement for responding to onNewIntent then Delphi 10 Seattle (and later) makes this something of a doddle.

I have updated the article to show the more direct means of accessing NFC from Delphi 10 Seattle and later and provided corresponding updated samples for Delphi 10 Seattle and Delphi 10.1 Berlin. You can find the full details and code there.

In short, though, new intents are picked up in the FireMonkey activity’s onNewIntent method and sent into the RTL cross-platorm messaging system. You can write message-receiving code to pick up new intents and process them, This intent messaging is done via an additional (and seemingly undocumented) use of the Android-specific TMessageReceivedNotification message.

If you are playing with NFC cards in your apps you can simplify the setup very nicely in the most recent versions of Delphi.

Just to round this topic (given the blog subject) off I’ll also mention that another activity callback, onActivityResult, is also covered by FireMonkey and has been since Delphi XE6. This is also wrapped up in the same messaging system, though this time with the TMessageResultNotification message.

I showed an example of how to use this to pick up information about a scanned barcode (scanned by the popular and open Source ZXing, or Zebra Crossing, barcode scanner app) in an article about launching various activity types back in April 2014: Launching activities and handling results in Delphi XE6 Android apps.

* Update [16/09/2016]: I have since discovered that both Delphi 10 Seattle and Delphi 10.1 Berlin ship with an Android intents demo that does in fact utilise this feature. The demo is located at:

  • Delphi 10 Seattle: %PUBLIC%\Documents\Embarcadero\Studio\17.0\Samples\Object Pascal\Mobile Snippets\AndroidIntents, documented here
  • Delphi 10.1 Berlin: %PUBLIC%\Documents\Embarcadero\Studio\18.0\Samples\Object Pascal\Mobile Snippets\AndroidIntents, documented here

The most up to date version of the demo can be found in the Delphi 10 Seattle Sourceforce repository or the Delphi 10.1 Berlin Sourceforge repository.


  1. [..] back in April 2014: Launching activities and handling results in Delphi 6 Android apps [..]

    you surely meant Delphi XE6 and not Delphi 6 ;-)

  2. Any idea why Delphi is still using the NDK version r9?
    Would not it be better to use the latest version (r13)?

    1. No I'm not sure. You can submit a bug report pointing out it is not up to date. And of course you can download r13, and point the IDE at the various updated tools, which might help you a tad. Though from the NDK, we're looking primarily at the linker (and the debugger sentinel), along with the library. I'm not sure the other bits are too important in terms of being right up to date. It would be a useful exercise to try the latest though.

  3. Hi.
    Can you please help me with problem (it's related to this topic ) - How to pass boolean (or integer) with PutExtra to intent and read it from Delphi?
    Here is the details

    Thank you!

    1. Please see