Java Question CallBack Handler

LuigiTasca

Member
Licensed User
Longtime User
Hi,
I have to integrate a 3th part library for POS comunication.
I can initialize libs objects and execute methods with JavaObject (like makePayment), but I can't intercept the callback event (makePayment_completed).
This should works with Android handler and HandlerMessages.
They told me that handler in java 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:
      myServiceHandler.InitializeNewInstance("android.os.Handler", Array As Object())[/INDENT]
      msg_handler = myServiceHandler.CreateEventFromUI("android.os.Handler.Callback", "Callback", Null)
                      
      'Eseguo register
      myPOSServiceObj.RunMethod("register",Array As Object(myServiceHandler))
                      
      'Executes the method that should raise my event after a while
      myPOSServiceObj.RunMethod("makePayment",Array As Object("1",TotIntParse,Null))

The callbacksObject (in our case "myServiceHandler") 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."

But Callback_Event is not raised.
Do you see something wrong?

Thank you.
 

Semen Matusovskiy

Well-Known Member
Licensed User
Why not to use inline Java ?
Simple callback. Touch red zone and look log.

B4X:
Sub Process_Globals
   
End Sub

Sub Globals
    Dim Label1 As Label   
End Sub

Sub Activity_Create (FirstTime As Boolean)

    Activity.Color = Colors.White
    Label1.Initialize ("")
    Activity.AddView (Label1, 5%x, 5%y, 90%x, 40dip)
    Label1.Color = Colors.Red   
   
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod ("SetTouchListener", Array (Label1))
   
End Sub

Sub Label_onTouch (x As Float, y As Float)
   
    Log ("x = " & x & " y = " & y)
   
End Sub

#If Java
import android.view.View;
import android.view.MotionEvent;

public void SetTouchListener (View myView)
    {
    myView.setOnTouchListener
        (
        new View.OnTouchListener ()
            {
            @Override
            public boolean onTouch (View v, MotionEvent event)
                {
                if (activityBA.subExists ("label_ontouch"))
                    {
                    activityBA.raiseEvent (activityBA, "label_ontouch", event.getX (), event.getY ());
                    }
                return false;
                }
            }   
        );
    }

#End If
 
Last edited:
Top