How to call Java methods from LCB?

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
TorstenHolmer
Posts: 40
Joined: Mon Oct 28, 2013 1:23 pm
Location: Dresden, Germany

How to call Java methods from LCB?

Post by TorstenHolmer » Fri Jun 11, 2021 3:20 pm

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

PaulDaMacMan
Posts: 454
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: How to call Java methods from LCB?

Post by PaulDaMacMan » Wed Jun 16, 2021 1:34 am

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.
My LCB Repos: https://github.com/PaulMcClernan/
My LiveCode YouTube Videos: PlayList

Post Reply

Return to “LiveCode Builder”