Android Question 3th part library. Callback problem with annotations

Discussion in 'Android Questions' started by LuigiTasca, Oct 5, 2018.

  1. LuigiTasca

    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
    Code:
    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
     
  2. stevel05

    stevel05 Expert Licensed User

    Do you have the javadoc for the register method on the myjarservice object?
     
  3. LuigiTasca

    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.
     
  4. LuigiTasca

    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."
     
  5. stevel05

    stevel05 Expert Licensed User

    How did you try to call it with JavaObject? Do you have the fully qualified name of the required interface?
     
  6. LuigiTasca

    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).
     
  7. stevel05

    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.
     
  8. stevel05

    stevel05 Expert Licensed User

    What is the POS, is the API available online?
     
  9. LuigiTasca

    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.
    Code:
    @MyTaskCompleted
    public void onMyTaskCompleted()
    {
    ...
    }
    Raised on my activity

    Code:
    ' 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" , "")
     
  10. stevel05

    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:
    Code:
    Sub onMyTaskCompleted_Event(MethodName As String,Args() As Object) As Object
     
  11. LuigiTasca

    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
    Code:
    myjarService.CreateEventFromUI("com.company.classname.annotations.MyTaskCompleted""onMyTaskCompleted" , "")
    is still ok if the callback is raised on my activity? (me)
    Code:
    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?
     
  12. stevel05

    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?
     
  13. LuigiTasca

    LuigiTasca Member Licensed User

    Yes, I tried with "CreateEvent"... So you think that I shouldn't pass "Me" on "Register" method?
     
  14. stevel05

    stevel05 Expert Licensed User

    No you need to pass a callback object created with either CreateEventFromUI or CreateEvent.

    Just to be clear -
    Code:
    Dim O As Object = myjarService.CreateEventFromUI("com.company.classname.annotations.MyTaskCompleted""onMyTaskCompleted" , "")
    myjarService.RunMethod(
    "register",Array As Object(O))
     
  15. LuigiTasca

    LuigiTasca Member Licensed User

    I just tried... Nothing to do...
    Thank you anyway for your support
     
  16. stevel05

    stevel05 Expert Licensed User

    If you can't give out the proprietary information, you will need to ask the supplier for support.
     
  17. stevel05

    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.
     
  18. LuigiTasca

    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:
    Code:
    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:

    Code:
    [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.
     
  19. stevel05

    stevel05 Expert Licensed User

    The Line:

    Code:
    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))
     
  20. LuigiTasca

    LuigiTasca Member Licensed User

    It doesn't work. :( Thank you again
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice