Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
JavaClassWrapper
Hereda: Object
Proporciona acceso a la Interfaz Nativa de Java.
Descripción
El singleton JavaClassWrapper proporciona una forma para que la aplicación de Godot envíe y reciba datos a través de la Interfaz Nativa de Java (JNI).
Nota: Este singleton solo está disponible en compilaciones de Android.
var LocalDateTime = JavaClassWrapper.wrap("java.time.LocalDateTime")
var DateTimeFormatter = JavaClassWrapper.wrap("java.time.format.DateTimeFormatter")
var datetime = LocalDateTime.now()
var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")
print(datetime.format(formatter))
Advertencia: Al llamar a métodos Java, asegúrate de verificar get_exception() para comprobar si el método lanzó una excepción.
Tutoriales
Métodos
create_proxy(object: Object, interfaces: PackedStringArray) |
|
create_sam_callback(sam_interface: String, callable: Callable) |
|
Descripciones de Métodos
JavaObject create_proxy(object: Object, interfaces: PackedStringArray) 🔗
Creates a JavaObject implementing the given Java interfaces using the given Object as the implementation.
The object must contain methods signatures matching the methods signatures from the passed Java interfaces. Invoking methods from the Java interfaces will route to the matching object method.
class PrintProxy:
func println(content: String) -> void:
print(content)
var print_proxy = PrintProxy.new()
var printer_object = JavaClassWrapper.create_proxy(print_proxy, ["android.util.Printer"])
printer_object.println("Hello Godot World!")
Note: This method only works on Android. On every other platform, this method will always return null.
JavaObject create_sam_callback(sam_interface: String, callable: Callable) 🔗
Creates a JavaObject implementing the Java Single Abstract Method (SAM) interface using the Godot Callable as the implementation.
The sam_interface must be a Java SAM interface, meaning it must only have a single abstract method to implement.
The callable must be able to handle the same parameter types as the SAM interface method, and must provide the same return type. The callable will be invoked as a callback, passing the arguments from the Java SAM interface method.
var cb = func (content: String) -> void:
print(content)
var callback = JavaClassWrapper.create_sam_callback("android.util.Printer", cb)
callback.println("Hello Godot World!")
Note: This method only works on Android. On every other platform, this method will always return null.
JavaObject get_exception() 🔗
Devuelve la excepción Java de la última llamada a una clase Java. Si no hubo ninguna excepción, devolverá null.
Nota: Este método solo funciona en Android. En cualquier otra plataforma, este método siempre devolverá null.
JavaClass wrap(name: String) 🔗
Envuelve una clase definida en Java y la devuelve como un tipo Object JavaClass con el que Godot puede interactuar.
Al envolver clases internas (anidadas), utiliza $ en lugar de . para separarlas. Por ejemplo, JavaClassWrapper.wrap("android.view.WindowManager$LayoutParams") envuelve la clase WindowManager.LayoutParams.
Nota: Para invocar un constructor, llama a un método con el mismo nombre que la clase. Por ejemplo:
var Intent = JavaClassWrapper.wrap("android.content.Intent")
var intent = Intent.Intent()
Nota: Este método solo funciona en Android. En cualquier otra plataforma, este método no hace nada y devuelve una JavaClass vacía.