Hi,
I've to integrate my app with a library that manage the communication of a device with a POS.
I initialize my class object with javaObject and run methods without problems. But the lib needs a method "Register(callbacksClass)" where I pass my activity istance to manage the callbacks methods.
So in my activity I should have these methods with annotations.
@MyTaskCompleted
public void onMyTaskCompleted(){...}
That's my code
B4X:
Dim JO_1 AS JavaObject
JO_1 = JO_1.InitializeStatic("com.company.classname.classobject").RunMethod("getInstance",Null)
Result = JO_1.RunMethod("init",Array As Object(myProperties))
If Result = True Then
Dim StatusSuccess As Boolean
Dim myjarService As JavaObject
myjarService= JO_1.RunMethodJO("getService",Array As Object())
' here i send my istance
myjarService.RunMethod("register",Array As Object(Me))
'Then i try to create event with JO without success
End if
How can I handle this event?
I tried with JO.CreateEvent but it doesn't work. Anyway I think that CreateEvent is not ok for this kind of method.
Method register:
public void register(Object callbacksObject)
"[IT]Registra l’oggetto (una classe) nella quale cercare, tramite la reflection, le annotazioni i cui metodi
riceveranno le notifiche delle operazioni. "
"[EN]Record the object (a class) in which to search, through reflection, annotations whose methods
will receive notifications of operations."
Params:
callbacksObject: the object to connect with myjarservice.
"The callbacksObject variable passed to the register method is the instance of a class, it is a simple class that extends Application (Android class), but it could also be an Activity or a base class or this."
I can't post here the full name. I have no problems with the Register method but to intercept the callback event in the object what I pass (in my case my activity).
Without seeing more information it's impossible to help i'm afraid. If you have the name of the interface, then there should be no reason it wouldn't work with CreateEvent or CreateEventfromUI and JavaObject.
Just set it up the same as you would for any other callback.
The API is not avaiable online. I'm sorry but I can't post here the full name.
This should be the callback function on the event.
B4X:
@MyTaskCompleted
public void onMyTaskCompleted()
{
...
}
Raised on my activity
B4X:
' here i send my istance
myjarService.RunMethod("register",Array As Object(Me))
How can I intercept "onMyTaskCompleted"?
I tried with context CreateEventFromUI("com.company.classname.annotations.MyTaskCompleted", "onMyTaskCompleted" , "")
and with
myjarService.CreateEventFromUI("com.company.classname.annotations.MyTaskCompleted", "onMyTaskCompleted" , "")
Based on your second line of code, and assuming that "com.company.classname.annotations.MyTaskCompleted" is a valid interface (you would get an error telling you it's not), then the Sub that should be called would be:
B4X:
Sub onMyTaskCompleted_Event(MethodName As String,Args() As Object) As Object
is still ok if the callback is raised on my activity? (me)
B4X:
myjarService.RunMethod("register",Array As Object(Me))
'"The callbacksObject variable passed to the register method is the instance of a class, it is a simple class that extends Application (Android class), but it could also be an Activity or a base class or this."
No you need to pass a callback object created with either CreateEventFromUI or CreateEvent.
Just to be clear -
B4X:
Dim O As Object = myjarService.CreateEventFromUI("com.company.classname.annotations.MyTaskCompleted", "onMyTaskCompleted" , "")
myjarService.RunMethod("register",Array As Object(O))
Hi @stevel05 ,
I contacted the supplier of the library. They gave me an alternative lib without annotations. This works with Android handler and HandlerMessages.
They told me that in java it should be something like this:
B4X:
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// Check the id throught msg.what
// Get the obj throught msg.obj,
// ....
}
};
We tried to do this in b4a in this way:
B4X:
[INDENT]myServiceHandler.InitializeNewInstance("android.os.Handler", Array As Object())[/INDENT]
msg_handler = myServiceHandler.CreateEventFromUI("android.os.Handler.Callback", "Callback", Null)
'Eseguo register
myServiceObj.RunMethod("register",Array As Object(myServiceHandler))
myServiceObj.RunMethod("makePayment",Array As Object("1",TotIntParse,Null))
But Callback_Event is not raised.
Do you see something wrong?
myServiceHandler.InitializeNewInstance("android.os.Handler", Array As Object())
Should be
myServiceHandler.InitializeNewInstance("android.os.Handler", Null)
and the line :
myServiceObj.RunMethod("register",Array As Object(myServiceHandler))
Should be
myServiceObj.RunMethod("register",Array As Object(msg_handler))