Java – How to extend Android classes with Delphi xe5

How to extend Android classes with Delphi xe5… here is a solution to the problem.

How to extend Android classes with Delphi xe5

I’m trying to extend a class/interface from “Androidapi.JNI.App.pas” (TJservice), and I need to extend this class to create a service. Is it possible? What to do?

some example of java/android service

I need to override methods such as: oncreate, onbind, ondestroy. But I don’t know how.

I’ve tried this:

 type Tandroidservice= class(TJService)
  function onBind(intent: JIntent): JIBinder; override;
  procedure onCreate; override;
  procedure onDestroy; override;
  procedure onStart(intent: JIntent; startId: Integer); override;
 end;

This error occurs:

[DCC Error] Unit1.pas(13): E2137 Method 'onBind' not found in base class
[DCC Error] Unit1.pas(14): E2137 Method 'onCreate' not found in base class
[DCC Error] Unit1.pas(15): E2137 Method 'onDestroy' not found in base class
[DCC Error] Unit1.pas(16): E2137 Method 'onStart' not found in base class
[DCC Error] Unit1.pas(13): E2065 Unsatisfied forward or external declaration: 'Tandroidservice.onBind'
[DCC Error] Unit1.pas(14): E2065 Unsatisfied forward or external declaration: 'Tandroidservice.onCreate'
[DCC Error] Unit1.pas(15): E2065 Unsatisfied forward or external declaration: 'Tandroidservice.onDestroy'
[DCC Error] Unit1.pas(16): E2065 Unsatisfied forward or external declaration: 'Tandroidservice.onStart'

Solution

Your only option currently is to inherit from the Java class and then use JNI to call Delphi and switch threads to FMX threads.

My CodeRage 8 class (next week at the time of writing) shows how to do this with Activities. This is almost the same process.

Now this doesn’t help you, except to know that you can’t inherit directly from Java classes in Delphi for Android, although in Delphi for iOS you can inherit from Objective-C level.

Related Problems and Solutions