JavaClassWrapper: Add example to invoke constructor (#11155)

* JavaClassWrapper: Add example to invoke constructor
This commit is contained in:
Anish Mishra
2025-08-01 21:25:06 +05:30
committed by GitHub
parent 5a99b22902
commit f781468e76

View File

@@ -113,3 +113,23 @@ Java inner classes can be accessed using the ``$`` sign:
# Do something specific on android 11 devices.
else:
# All other devices
Example: Calling a constructor
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A constructor is invoked by calling a method with the same name as the class.
This example creates an intent to send a text:
.. code-block:: gdscript
# Retrieve the AndroidRuntime singleton.
var android_runtime = Engine.get_singleton("AndroidRuntime")
if android_runtime:
var Intent = JavaClassWrapper.wrap("android.content.Intent")
var activity = android_runtime.getActivity()
var intent = Intent.Intent() # Call the constructor.
intent.setAction(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_TEXT, "This is a test message.")
intent.setType("text/plain")
activity.startActivity(intent)