Search code examples
javaandroiddelphijava-native-interfacedelphi-10.2-tokyo

Pass Java .class file to Delphi Constructor


I'm working with JNI to access Android functions in Delphi 10.2 Tokyo. I need to pass a .class type as an argument for a Delphi function constructor.

The original Java method looks like this:

private Intent getServiceIntent() {
    return new Intent(this, ToyVpnService.class);
}

From the Android Documentation for the Intent constructor:

Intent(Context packageContext, Class<?> cls)

Create an intent for a specific component.

I have to use this constructor, I can't change it.

I need to translate this to Delphi, but I am stuck at passing the .class type. Right now, my code looks something like this:

function TForm1.GetServiceIntent: JIntent;
begin
  result := TJIntent.JavaClass.init(FContext, ???);
end;

I tried to use the ClassLoader from Java, but can't access the loadClass() function because it is protected so package only.

I tried to use Class.forName, but can't specify the Path from the .class file.

I added the .class file as a Delphi resource file and loaded it via TResourceStream, but couldn't find a solution to convert this input to a Java Class reference.


Solution

  • Based on this answer, I guess it should be something like

    Intent := TJIntent.JavaClass.init(SharedActivityContext,
      TJLang_Class.JavaClass.forName(StringToJString('com.example.ToyVpnService'),
      True, SharedActivity.getClassLoader));