Page 1 of 1

How to call Java methods from LCB?

Posted: Fri Jun 11, 2021 3:20 pm
by TorstenHolmer
HI,

I want to say to my Android device that it should make a very short vibration. I know that I have to write a Livecode Builder library which seems to be easy. BUT now I have the problem to transform some Java code I have found into the correct foreign handler.

Here is the Java code which seems to do what I want:

Code: Select all

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 4100 milliseconds
v.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE));
On the english Livecode Wiki are some examples for foreign bindings:
https://livecode.fandom.com/wiki/Foreig ... gs_to_Java

Binding to a class constructor with parameters:

Code: Select all

foreign handler CreateJavaString(in pBytes as JByteArray) returns JString binds to "java:java.lang.String>new([B)"
My question is: how to transform the Java code above into the foreign handler format?

Cheers,
Torsten

Re: How to call Java methods from LCB?

Posted: Wed Jun 16, 2021 1:34 am
by PaulDaMacMan
My Java / Android LCB is not good at all but I THINK that the binding string for that first line would look something like this:

Code: Select all

__safe foreign handler _JNI_getSystemService(in pContext as JObject) returns optional JObject to "java:androidr>getSystemService()V" -- the 'optional' is if the function can return null/void. 
but I haven't played around much at all with LCB FFI in conjunction with Android/JAVA

I would look at the android LCB sample code included with LiveCode IDE and the few of other examples in the wild if you haven't already.