Name
CallJavaMethod -- call method of Java activity (V8.0)
Synopsis
[ret] = CallJavaMethod(name$[, t, type1, value1, type2, value2, ...])
Platforms
Android only

Function
This is a powerful function that allows you to call directly into the Java code of Hollywood's Android activity. The Java code may then access the whole Android API to enhance your app with custom features unavailable in Hollywood.

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:
Set this to True if the method you'd like to call is static. By default, CallJavaMethod() expects the method to be non-static.

ReturnType:
Set this tag to configure the return data type for the method passed in name$. This must be one of the following predefined constants:

#BYTE:
Java's byte data type, a signed 8-bit quantity.
#SHORT:
Java's short data type, a signed 16-bit quantity.
#INTEGER:
Java's int data type, a signed 32-bit quantity.
#FLOAT:
Java's float data type, a 32-bit floating point number.
#DOUBLE:
Java's double data type, a 64-bit floating point number.
#BOOLEAN:
Java's boolean data type, a boolean value (True or False).
#STRING:
Java's String data type, a text string.
#VOID:
No return value.

This tag defaults to #VOID, i.e. the method doesn't return any value.

ReturnArray:
If this tag is set to 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:
Java's byte data type, a signed 8-bit quantity.
#SHORT:
Java's short data type, a signed 16-bit quantity.
#INTEGER:
Java's int data type, a signed 32-bit quantity.
#FLOAT:
Java's float data type, a 32-bit floating point number.
#DOUBLE:
Java's double data type, a 64-bit floating point number.
#BOOLEAN:
Java's boolean data type, a boolean value (True or False).
#STRING:
Java's 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.

Inputs
name$
name of method to call
t
optional: table configuring further options (see above)
type1
optional: type of first parameter to pass to method (see above for possible values)
value1
optional: actual value of first parameter
...
optional: unlimited number of method parameters
Results
ret
optional: in case ReturnType is not #VOID, the value returned by the Java method

Show TOC