[ret] = CallJavaMethod(name$[, t, type1, value1, type2, value2, ...])
You have to pass the name of the method to call in the name$
argument. Note that Java is
a case-sensitive language so the method name you pass in name$
must exactly match
its definition in the Java code.
Optionally, you can pass a table in the second argument. This table currently supports the following tags:
Static:
True
if the method you'd like to call is static. By default, CallJavaMethod()
expects the method to be non-static.
ReturnType:
name$
. This
must be one of the following predefined constants:
#BYTE:
byte
data type, a signed 8-bit quantity.
#SHORT:
short
data type, a signed 16-bit quantity.
#INTEGER:
int
data type, a signed 32-bit quantity.
#FLOAT:
float
data type, a 32-bit floating point number.
#DOUBLE:
double
data type, a 64-bit floating point number.
#BOOLEAN:
boolean
data type, a boolean value (True
or False
).
#STRING:
String
data type, a text string.
#VOID:
This tag defaults to #VOID
, i.e. the method doesn't return any value.
ReturnArray:
True
, the method passed in name$
is expected to return
an array of the data type specified in ReturnType
. Note that if ReturnArray
is set to True
, ReturnType
must not be set to #VOID
. Defaults to False
.
After the optional table argument, CallJavaMethod()
accepts an unlimited number of
type
and value
pairs. These pairs can be used to pass parameters to the method
specified in name$
. For each type
there must be a corresponding value
argument
directly after it.
The following predefined constants are currently supported for the type
argument:
#BYTE:
byte
data type, a signed 8-bit quantity.
#SHORT:
short
data type, a signed 16-bit quantity.
#INTEGER:
int
data type, a signed 32-bit quantity.
#FLOAT:
float
data type, a 32-bit floating point number.
#DOUBLE:
double
data type, a 64-bit floating point number.
#BOOLEAN:
boolean
data type, a boolean value (True
or False
).
#STRING:
String
data type, a text string.
The value that follows each type
argument must correspond to the type specified for
that parameter, e.g. if you pass #STRING
in a type
argument, a string must follow
after the #STRING
argument.
Here is an example Java method:
public int littleTest(String s, int v) { Log.v("Test", "Got data: " + s + " " + v); return 50; } |
Note that it is important to declare the method using the public
keyword because it
is accessed from outside its class. To call the Java method littleTest()
from your
Hollywood script using CallJavaMethod()
, you'd have to use the following code:
r = CallJavaMethod("littleTest", {ReturnType = #INTEGER}, #STRING, "Hello Java!", #INTEGER, 10) |
Since we have declared the Java method as returning an integer value and its implementation
on the Java side returns 50, the Hollywood variable r
will be set to 50 as soon as
CallJavaMethod()
returns.
Note that this function can only be used in connection with the Hollywood APK Compiler because the Hollywood Player doesn't allow you to inject any custom code into its activity. This is only supported by the Hollywood APK Compiler.
The Java methods you declare will all be part of a subclass of Hollywood's Android
Activity
. Thus, you can call any of the Activity
methods directly from the methods
called by CallJavaMethod()
. Keep in mind, though, that Java methods run by CallJavaMethod()
will not be executed on the main (UI) thread but on Hollywood's VM thread. So if you
need to access Android APIs that can only be called from the UI thread (like most of
the View-related
APIs), you must first delegate from the Hollywood thread to the main
thread or the code won't work.
ReturnType
is not #VOID
, the value returned
by the Java method