Android Question 3th part library. Callback problem with annotations

LuigiTasca

Member
Licensed User
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.

Thank you
 

stevel05

Expert
Licensed User
Do you have the javadoc for the register method on the myjarservice object?
 
Upvote 0

LuigiTasca

Member
Licensed User
This is my documentation:

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.
 
Upvote 0

LuigiTasca

Member
Licensed User
"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."
 
Upvote 0

stevel05

Expert
Licensed User
How did you try to call it with JavaObject? Do you have the fully qualified name of the required interface?
 
Upvote 0

LuigiTasca

Member
Licensed User
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).
 
Upvote 0

stevel05

Expert
Licensed User
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.
 
Upvote 0

stevel05

Expert
Licensed User
What is the POS, is the API available online?
 
Upvote 0

LuigiTasca

Member
Licensed User
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" , "")
 
Upvote 0

stevel05

Expert
Licensed User
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
 
Upvote 0

LuigiTasca

Member
Licensed User
It is a valid interface because I don't get errors... and in my code I have the same sub :(
Thank you anyway...

Are you sure that
B4X:
myjarService.CreateEventFromUI("com.company.classname.annotations.MyTaskCompleted", "onMyTaskCompleted" , "")

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."
or I should use something else?
 
Upvote 0

stevel05

Expert
Licensed User
No, it won't work using Me as you can't format the code in the module in the required manner.

Did you try CreateEvent instead of CreateEventFromUI?
 
Upvote 0

stevel05

Expert
Licensed User
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))
 
Upvote 0

stevel05

Expert
Licensed User
If you can't give out the proprietary information, you will need to ask the supplier for support.
 
Upvote 0

stevel05

Expert
Licensed User
One further thought, you may be able to do it using inline java, but again, without documentation I wouldn't know how achievable it would be.
 
Upvote 0

LuigiTasca

Member
Licensed User
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?

Thank you.
 
Upvote 0

stevel05

Expert
Licensed User
The Line:

B4X:
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))
 
Upvote 0
Top