Recent

Author Topic: Android JNI Bridge  (Read 4247 times)

AliZairov

  • New Member
  • *
  • Posts: 18
Android JNI Bridge
« on: April 30, 2017, 04:45:59 pm »
Hi. This example is the Delphi Android bridge system.

Code: Pascal  [Select][+][-]
  1. unit JavaInterfaces;
  2.  
  3. interface
  4.  
  5. uses
  6.   Androidapi.JNIBridge,
  7.   Androidapi.JNI.JavaTypes;
  8.  
  9. type
  10. // ===== Forward declarations =====
  11.  
  12.   JAppLibrary = interface;//com.zairovsoft.AppLibrary
  13.  
  14. // ===== Interface declarations =====
  15.  
  16.   JAppLibraryClass = interface(JObjectClass)
  17.     ['{3252901E-D765-4AC5-8FB7-FC7F2CA589C6}']
  18.     {class} function GetMsg(P1: JString): JString; cdecl;
  19.     {class} function init: JAppLibrary; cdecl;
  20.   end;
  21.  
  22.   [JavaSignature('com/zairovsoft/AppLibrary')]
  23.   JAppLibrary = interface(JObject)
  24.     ['{B4900D81-BD6D-4163-AC93-A7A62C496A6C}']
  25.   end;
  26.   TJAppLibrary = class(TJavaGenericImport<JAppLibraryClass, JAppLibrary>) end;
  27.  
  28. implementation
  29.  
  30. procedure RegisterTypes;
  31. begin
  32.   TRegTypes.RegisterType('JavaInterfaces.JAppLibrary', TypeInfo(JavaInterfaces.JAppLibrary));
  33. end;
  34.  
  35. initialization
  36.   RegisterTypes;
  37.  
  38. end.
  39.  

Code: Pascal  [Select][+][-]
  1. package com.zairovsoft;
  2.  
  3. public class AppLibrary
  4. {
  5.         public static String GetMsg(String value)
  6.         {
  7.                 return "AppLibrary: " + value;
  8.         }
  9. }
  10.  

Code: Pascal  [Select][+][-]
  1. ShowMessage(JStringToString(TJAppLibrary.JavaClass.GetMsg(StringToJString(Edit1.Text))));
  2.  

This is more useful. How would such a similar structure for Pascal be?

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Android JNI Bridge
« Reply #1 on: May 17, 2017, 02:43:11 pm »
JNI is done in Free Pascal like this:

http://wiki.lazarus.freepascal.org/Android_Programming#Android_JNI

Does this answer your question?

AliZairov

  • New Member
  • *
  • Posts: 18
Re: Android JNI Bridge
« Reply #2 on: May 17, 2017, 03:48:13 pm »
Hi. Thank you. I will examine.

AliZairov

  • New Member
  • *
  • Posts: 18
Re: Android JNI Bridge
« Reply #3 on: December 11, 2020, 10:46:26 pm »
Hello. I didn't want to open a new topic.

How do I do a dynamic structure like this?
Code: Pascal  [Select][+][-]
  1. // Generated by JavaToPas v1.5 20150830 - 103113
  2. ////////////////////////////////////////////////////////////////////////////////
  3. unit android.widget.Toast;
  4.  
  5. interface
  6.  
  7. uses
  8.   AndroidAPI.JNIBridge,
  9.   Androidapi.JNI.JavaTypes,
  10.   Androidapi.JNI.GraphicsContentViewText;
  11.  
  12. type
  13.   JToast = interface;
  14.  
  15.   JToastClass = interface(JObjectClass)
  16.     ['{3DA2EC00-C2D0-4434-A7F1-708100694351}']
  17.     function _GetLENGTH_LONG : Integer; cdecl;                                  //  A: $19
  18.     function _GetLENGTH_SHORT : Integer; cdecl;                                 //  A: $19
  19.     function getDuration : Integer; cdecl;                                      // ()I A: $1
  20.     function getGravity : Integer; cdecl;                                       // ()I A: $1
  21.     function getHorizontalMargin : Single; cdecl;                               // ()F A: $1
  22.     function getVerticalMargin : Single; cdecl;                                 // ()F A: $1
  23.     function getView : JView; cdecl;                                            // ()Landroid/view/View; A: $1
  24.     function getXOffset : Integer; cdecl;                                       // ()I A: $1
  25.     function getYOffset : Integer; cdecl;                                       // ()I A: $1
  26.     function init(context : JContext) : JToast; cdecl;                          // (Landroid/content/Context;)V A: $1
  27.     function makeText(context : JContext; resId : Integer; duration : Integer) : JToast; cdecl; overload;// (Landroid/content/Context;II)Landroid/widget/Toast; A: $9
  28.     function makeText(context : JContext; text : JCharSequence; duration : Integer) : JToast; cdecl; overload;// (Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast; A: $9
  29.     procedure cancel ; cdecl;                                                   // ()V A: $1
  30.     procedure setDuration(duration : Integer) ; cdecl;                          // (I)V A: $1
  31.     procedure setGravity(gravity : Integer; xOffset : Integer; yOffset : Integer) ; cdecl;// (III)V A: $1
  32.     procedure setMargin(horizontalMargin : Single; verticalMargin : Single) ; cdecl;// (FF)V A: $1
  33.     procedure setText(resId : Integer) ; cdecl; overload;                       // (I)V A: $1
  34.     procedure setText(s : JCharSequence) ; cdecl; overload;                     // (Ljava/lang/CharSequence;)V A: $1
  35.     procedure setView(view : JView) ; cdecl;                                    // (Landroid/view/View;)V A: $1
  36.     procedure show ; cdecl;                                                     // ()V A: $1
  37.     property LENGTH_LONG : Integer read _GetLENGTH_LONG;                        // I A: $19
  38.     property LENGTH_SHORT : Integer read _GetLENGTH_SHORT;                      // I A: $19
  39.   end;
  40.  
  41.   [JavaSignature('android/widget/Toast')]
  42.   JToast = interface(JObject)
  43.     ['{47C787A5-0B8C-4FFB-B2C5-E625822B718B}']
  44.     function getDuration : Integer; cdecl;                                      // ()I A: $1
  45.     function getGravity : Integer; cdecl;                                       // ()I A: $1
  46.     function getHorizontalMargin : Single; cdecl;                               // ()F A: $1
  47.     function getVerticalMargin : Single; cdecl;                                 // ()F A: $1
  48.     function getView : JView; cdecl;                                            // ()Landroid/view/View; A: $1
  49.     function getXOffset : Integer; cdecl;                                       // ()I A: $1
  50.     function getYOffset : Integer; cdecl;                                       // ()I A: $1
  51.     procedure cancel ; cdecl;                                                   // ()V A: $1
  52.     procedure setDuration(duration : Integer) ; cdecl;                          // (I)V A: $1
  53.     procedure setGravity(gravity : Integer; xOffset : Integer; yOffset : Integer) ; cdecl;// (III)V A: $1
  54.     procedure setMargin(horizontalMargin : Single; verticalMargin : Single) ; cdecl;// (FF)V A: $1
  55.     procedure setText(resId : Integer) ; cdecl; overload;                       // (I)V A: $1
  56.     procedure setText(s : JCharSequence) ; cdecl; overload;                     // (Ljava/lang/CharSequence;)V A: $1
  57.     procedure setView(view : JView) ; cdecl;                                    // (Landroid/view/View;)V A: $1
  58.     procedure show ; cdecl;                                                     // ()V A: $1
  59.   end;
  60.  
  61.   TJToast = class(TJavaGenericImport<JToastClass, JToast>)
  62.   end;
  63.  
  64. const
  65.   TJToastLENGTH_LONG = 1;
  66.   TJToastLENGTH_SHORT = 0;
  67.  
  68. implementation
  69.  
  70. end.
  71.  

This code usage style is longer
Code: Pascal  [Select][+][-]
  1. procedure Toast(instance: JObject; Str: JString; Duration: JInt);
  2. //const
  3.   //LENGTH_SHORT = 0;
  4.   //LENGTH_LONG = 1;
  5. var
  6.   env: PJNIEnv;
  7.   clz: JClass;
  8.   mtd, mtd2: JMethodID;
  9.   ptr: array[0..2] of JValue;
  10.   obj: JObject;
  11. begin
  12.   env := TJNIHelper.GetJNIEnv;
  13.   clz := TJNIHelper.GetClassID('android/widget/Toast');
  14.   mtd := TJNIHelper.GetStaticMethodID(clz, 'makeText', '(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;');
  15.   //if (mtd = nil) then TLog.D('method not found'); exit;
  16.   ptr[0].l := instance;
  17.   ptr[1].l := Str;
  18.   ptr[2].i := Duration;
  19.   obj := env^^.CallStaticObjectMethodA(env, clz, mtd, @ptr);
  20.   mtd2 := TJNIHelper.GetMethodID(clz, 'show', '()V');
  21.   env^^.CallVoidMethod(env, obj, mtd2);
  22. end;
  23.  
  24. //Toast(instance, TJNIHelper.GetJString('CPU: ' + TJNIHelper.GetString(TAndroid.CPU)), 0);
  25.  

 

TinyPortal © 2005-2018