Tuesday, 3 February 2015

Delphi and Android services (part 3)

Once upon a time I uploaded a Delphi sample app to the Google Play Store, written in Delphi XE5 and implementing an Android service. I wrote about the published app in this post and supplied the source in this follow-up post.

There was an issue that’s been hanging around with that sample project. When pulled into Delphi XE7 and updated so it all compiled nicely, it didn’t play ball when deployed. Instead it hung.

I’ve eventually worked through the to-do list enough to take a look at the problem. For one reason or another starting the service in the OnCreate event handler caused an issue thanks to some required thread synchronisation, which no longer functions as I wanted it to.

I’ve worked around the problem by very slightly delaying the service starting by use of a one-shot timer. Then the form setup is done and dusted by the time the service is invoked and all seems to be well.

An updated download archive is available here, which contains the original Delphi XE5 project and a Delphi XE7 equivalent, which should now be functionally equivalent.

Another update has been made to the project download, which now contains Delphi XE8 support. Delphi 10 Seattle introduces its own support for native Android services, but Delphi XE8 / Lollipop made some changes that broke the demo as it was originally written. The XE8 version of the project should hopefully resolve that problem.

36 comments:

  1. Mr Brian,

    Based on your blog posts I created a extend class to gcmreciever.
    I compiled and add the jar library in XE7.

    But, I have noted my receiver class is not used when I receive a push message.

    I thhink is a problem on android manifest file.

    Please, does some can help on settings android manifest.

    public class GCMReceiverTest extends BroadcastReceiver
    {
    static final String TAG = "GCMReceiverTest";
    private static final int NOTIFY_ME_ID=1337;
    final static String GROUP_KEY_MSG = "group_key_msg";

    @Override
    public void onReceive(final Context context, final Intent receivedIntent)
    {
    String newMessage = receivedIntent.getExtras().getString("message");
    int icon = R.drawable.stat_notify_chat;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, "Wake up!", when);
    Intent notificationIntent = new Intent();
    notificationIntent.setClassName(context, "com.embarcadero.firemonkey.FMXNativeActivity");
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    //notificationIntent.putExtra("data1", "My Data 1");
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, "Nova Mensagem:", newMessage, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
    }
    }

    I have used:










    But it doesn't work

    ReplyDelete
    Replies
    1. I gather there is a required entry in the manifest file. I haven't worked in this area, but something along these lines may be what is missing. Hopefully you'll have already worked this out for yourself:

      Delete
    2. Ah, I see whatever it was that I posted at the end of my prior reply just above got stripped out by the comment engine when I posted, thanks perhaps to it being XML. Shame, I currently don't know what is missing...

      Delete
  2. Hello Brian,
    is it normal that i can't download the archive?
    it say 404 page not found. :\

    ReplyDelete
    Replies
    1. Very much *not* normal. My web site has been taken down by the provider thanks to over-use! I should have it back online within a few days - I've been slowed by being away busy with work.....

      Delete
    2. Ok, so it's partly back up - with the www. prefix required. So you can pull any files down, so long as you make sure the www. is on the front of the URL.

      Gak! These issues are annoying. My email went down as of yesterday as well. Hopefully should be back up soon though!

      Delete
    3. Great! i can download it but ican't make it work with XE8..
      did you try it on XE8?

      Delete
    4. Ah yeah, i eventually got it fully back online. Was significantly more trouble than it should have been. hey ho.

      Hmm, have I tried it with XE8? I think likely not. I seem to recall having to play about to get it working in XE7. I'll try and have a go with XE8 over the next few days.
      What symptoms do you get?
      Have you seen the errors arising in the Android monitor?
      Are you even successfully compiling the Java and building/installing the project?

      Delete
    5. Hi Yoni, an XE8 updated project has been uploaded now. See the update in the post.

      Delete
  3. yeah i can compile the java successfully.
    but i get this error on delphi ide
    "project serviceApp.apk raised exception class avorted (6)"

    ReplyDelete
    Replies
    1. Looks like the same issue as below maybe.

      Delete
  4. Brian, in XE8 and Lolipop 5.0.2 device. The method TJNIResolver.GetJavaClassID('com.blong.test.SampleService'); call exception Class Aborted (6) ... Please Help.

    ReplyDelete
    Replies
    1. I need to get this compiled in XE8 and see what results I get. I should be able to get on top of checking this out over the next few (working) days hopefully.

      Delete
    2. In fact the problem is in the Art in android Lolipop the class name must be qualified, rather than 'com.blong.test.SampleService' should be 'with / blong / test / SampleService', this solves the problems of RegisterNavive, there are still others, I am debugging to solve and share.

      Delete
    3. That sounds likely - someone else had mentioned this as an issue, but I hadn't seen it as an issue on my device. I will retest on a different Lollipop device and see what happens.
      Thanks for sharing your findings.

      Delete
    4. I change is the same as above but cannot run application on Lolipop 5.0.1

      Delete
    5. Hi Saulo, an XE8 updated project has been uploaded now. See the update in the post.

      Delete
    6. Congratulations, perfect ... in Seatle too ... (Native service seatle have serius bug's ... Thanks. It is Fantastic now;

      Delete
    7. Specifically, which issues have you hit with it, Saulo?
      When you say serious bugs, can you elaborate? Have other people hit them? Are they being discussed in forums? Are they in QC or whatever the current bug system is called?

      Delete
  5. cannot run is the same as above
    I change is the same as above but cannot run

    ReplyDelete
    Replies
    1. Hi, try the update I just posted. That seems to address the issues.

      Delete
  6. Hi, I had this error:
    Checking project dependencies...
    Compiling ServiceApp.dproj (Debug, Android)
    [Exec Error] The command "PATH C:\Program Files\Java\jdk1.8.0_25\bin;C:\Users\Public\Documents\Embarcadero\InterBase\redist\InterBaseXE7\IDE_spoof;E:\Datos\Embarcadero\Studio\16.0\bin;C:\Users\Public\Documents\Embarcadero\Studio\16.0\Bpl;E:\Datos\Embarcadero\Studio\16.0\bin64;C:\Users\Public\Documents\Embarcadero\Studio\16.0\Bpl\Win64;E:\Program Files (x86)\CollabNet;E:\Datos\Embarcadero\Studio\15.0\bin;C:\Users\Public\Documents\Embarcadero\Studio\15.0\Bpl;E:\Datos\Embarcadero\Studio\15.0\bin64;C:\Users\Public\Documents\Embarcadero\Studio\15.0\Bpl\Win64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Broadcom\Broadcom 802.11\Driver;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Java\jdk1.7.0_25\bin;E:\Datos\Android\android-sdk\build-tools\build-tools-21.1.2;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\ & "E:\Datos\Android\android-sdk\build-tools\17.0.0\dx.bat" --dex --output="C:\Users\fabian\Documents\Embarcadero\Studio\Projects\delphiServiceXe8\Android\Debug\Delphi_Service-dexed.jar" "C:\Users\fabian\Documents\Embarcadero\Studio\Projects\delphiServiceXe8\java\output\jar\Delphi_Service.jar"" exited with code 1.
    Failed
    Elapsed time: 00:00:01.4
    --
    Can you help me?

    ReplyDelete
    Replies
    1. It's a bit unclear. This is an error compiling the project in the IDE?
      Have you followed the Readme steps and used the build batch file to create the Delphi_Service jar file from the Java source?
      And to be clear you are running XE7 and hoping to target which Android version?

      Delete
  7. Brian, you use the delphi seatle android services ? No problem ?

    ReplyDelete
    Replies
    1. I have not yet had chance to look at them. I understand they generally work OK, but there are 1 or 2 small known config things, that can be worked around but which are not really big issues.

      Delete
  8. hey brian... great post....

    I am working wth seattle... which is great but... there is an issue... the service wont do anything!!

    I put a location sensor component... and nothing happens.

    any clue?

    ReplyDelete
    Replies
    1. Alas I haven't had the opportunity to get involved with DX as yet, though I hear there are some minor gremlins with the service support. Have you followed Jim McKeet's video examples on DX services to see if you can at least match his progress?
      Basically, my current advice is if you can't get the DX built-in stuff working, post on the forums and try my example framework in the meantime. I'm sure it will work out in due course, though!

      Delete
    2. Do check out http://cc.embarcadero.com/item/30432 and http://delphi.org/2015/10/android-services-presentations-and-workarounds

      Delete
    3. Hi! I had the same problem so I opened an issue on quality.embarcadero.com . The issue has been recognized with Majority prior. If you want, you can vote this issue so the internal team understands that the community want a rapid fix. Here the link https://quality.embarcadero.com/browse/RSP-12635

      Delete
    4. Thanks Daniele, yes I see it's logged with Major priority, so hopefully it will get fixed soon.

      Delete
    5. I found out a solution! Have a look here http://www.danielespinetti.it/2016/01/using-locationsensor-on-android-service.html

      Delete
    6. Oh, very good research and problem-solving - nice work guys!

      Delete
  9. I tried compiling it with tokio and it gives me this error in ServiceU.pas:

    [DCC Error] ServiceU.pas(231): E2003 Undeclared identifier: 'GetAndroidApp'

    regards.

    ReplyDelete