Java Question initialize external library with android.app.Activity and android.os.Handler objects

dune3000

Member
Licensed User
Longtime User
I'm new to B4A. I need to use an external USB printer library for my program.
The external library has a constructor as follow:

public UsbController(android.app.Activity activity, android.os.Handler handler){...}

Therefor I created my own library wrapper defined as follow:

@ShortName("UsbController")
@Version(0.4F)
public class UsbControllerWrapper {
public UsbControllerWrapper() {...}
public void initialize(android.app.Activity activity, android.os.Handler handler){...}
...
}

Now, I've tried many things in B4A but could not get it to work :BangHead: . How do I create and pass the activity and handler parameters to initialize this external library? Please help.

Thanks.
 

stevel05

Expert
Licensed User
Longtime User
Have you looked at the Library Developers Questions forum and particularly Creating Libraries for Basic4Android, you'll probably find some answers here. Have a look at the BA object and @ActivityObject
 

agraham

Expert
Licensed User
Longtime User
Try something like this
B4X:
@ActivityObject  // make sure we belong to an Activity
@ShortName("UsbController")
@Version(0.4F)
public class UsbControllerWrapper
{

  BA myba;

  public UsbControllerWrapper() {...}

  public void initialize(Ba ba) // ba is a hidden parameter
  {
    myba = ba; //save it if you need to 
    Actvity a = myba.activity; // get the Activity
    Handler h = new Handler(); // get a handler for the current thread  
    ...
  }
 ...
 }
 

dune3000

Member
Licensed User
Longtime User
Thanks, it works. This solved the problem of passing the Activity object to the library. Any idea how to create and pass the handler parameter? The handler is basically used as a callback to allow user program to handle the USB events.
 

dune3000

Member
Licensed User
Longtime User
I mean defining a handler in B4A(maybe a Sub handler(...), but I'm not sure) and pass to the library, not in java wrapper code as showed the above. The handler will do things like enable the "connect" button in UI (which is in B4A code) when the USB printer is detected, or perform clean up when the printer is disconnected etc.
 

agraham

Expert
Licensed User
Longtime User
Top