FireMonkey Android – handle callback message (JHandle) of JAR object

  

I have a big problem for a long time – I am looking for a solution everywhere, but after really many hours of testing I can not find a sensible solution.

I have a JAR library for the Zebra printer and a PAS file for it (http://aboutit.pl/MobilePrinter.zip). I need a piece of code in Delphi that will handle a callback (JHandle) single procedure from a JAR library.

Something like this:

TPrintCallback = class (TJavaLocal, JHandler_Callback)
function handleMessage (msg: JMessage): Boolean; cdecl;
end;

TfrmMain = class (TForm)
[..]
private
FCallback: TPrintCallback;
FHandler: JHandler;
end;

[…]

procedure TfrmMain.FormCreate (Sender: TObject);
begin
FCallback: = TPrintCallback.Create;
FHandler: = TJHandler.JavaClass.init (FCallback);
FPrinter1: = TJMobilePrinter.JavaClass.Init (TAndroidHelper.Context,FHandler, TJLooper.JavaClass.getMainLooper);
[…]
end;

function TPrintCallback.handleMessage (msg: JMessage): Boolean;
begin
Result: = True;
ShowMessage (msg.what.ToString + ” + msg.arg1.ToString);
end;

I would like the handleMessage function to receive messages, e.g. about the printer’s connection. Of course, the printer object can do it, but messages do not reach the Delphi application.

I also tried the RegisterNatives method, but also without success

procedure RegisterN;
var
PEnv: PJNIEnv;
ReceiverClass: JNIClass;
NativeMethod: JNINativeMethod;
begin
try
PEnv := TJNIResolver.GetJNIEnv;
ReceiverClass := TJNIResolver.GetJavaClassID(‘com/zebra/printer/service/PrinterHandler’);
NativeMethod.Name := ‘handleMessage’;
NativeMethod.Signature := ‘(Landroid/os/Message;)V’;
NativeMethod.FnPtr := @handleMessage;

PEnv^.RegisterNatives(PEnv, ReceiverClass, @NativeMethod, 1);
PEnv^.DeleteLocalRef(PEnv, ReceiverClass);
except
on E: Exception do
ShowMessage(E.Message);
end;
end;

I am ready to pay for any hint, where to look for a solution to this problem – I am in desperation. 😉

Edit:

In Java it’s look like this:

mMobilePrinter = new MobilePrinter(this, mHandler, null);

private final Handler mHandler = new Handler() {
@SuppressWarnings(“unchecked”)
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MobilePrinter.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case MobilePrinter.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
mListView.setEnabled(true);
mIsConnected = true;
invalidateOptionsMenu();
break;

case MobilePrinter.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;

case MobilePrinter.MESSAGE_PRINT_COMPLETE:
Toast.makeText(getApplicationContext(), “Complete to print”, Toast.LENGTH_SHORT).show();
break;

}
}
};

I need to have procedure handleMessage(Message msg) in Delphi…

Comments are closed.