QAndroidJniEnvironment Class

The QAndroidJniEnvironment provides access to the JNI Environment. More...

Header: #include <QAndroidJniEnvironment>
qmake: QT += androidextras
Since: Qt 5.2

This class was introduced in Qt 5.2.

Public Functions

QAndroidJniEnvironment()
~QAndroidJniEnvironment()
jclass findClass(const char *className)
JNIEnv *operator JNIEnv *() const
JNIEnv *operator->()

Static Public Members

JavaVM *javaVM()

Detailed Description

Member Function Documentation

QAndroidJniEnvironment::QAndroidJniEnvironment()

Constructs a new QAndroidJniEnvironment object and attach the current thread to the Java VM.

 bool exceptionCheck()
 {
     /*
       The QAndroidJniEnvironment attaches the current thread to the JavaVM on
       creation and detach when it goes out of scope.
      */
     QAndroidJniEnvironment qjniEnv;
     return qjniEnv->ExceptionCheck();
 }

QAndroidJniEnvironment::~QAndroidJniEnvironment()

Detaches the current thread from the Java VM and destroys the QAndroidJniEnvironment object.

jclass QAndroidJniEnvironment::findClass(const char *className)

Searches for className using all available class loaders. Qt on Android uses a custom class loader to load all the .jar files and it must be used to find any classes that are created by that class loader because these classes are not visible in the default class loader.

Returns the class pointer or null if is not found.

A use case for this function is searching for a custom class then calling its memeber method. The following code snippet create an instance of the class CustomClass and then calls printFromJava() method:

 QAndroidJniEnvironment env;
 jclass javaClass = env.findClass("org/qtproject/example/qtandroidextras/CustomClass");
 QAndroidJniObject classObject(javaClass);

 QAndroidJniObject javaMessage = QAndroidJniObject::fromString("findClass example");
 classObject.callMethod<void>("printFromJava",
                              "(Ljava/lang/String;)V",
                              javaMessage.object<jstring>());

This function was introduced in Qt 5.12.

[static] JavaVM *QAndroidJniEnvironment::javaVM()

Returns the Java VM interface.

JNIEnv *QAndroidJniEnvironment::operator JNIEnv *() const

Returns the JNI Environment pointer.

JNIEnv *QAndroidJniEnvironment::operator->()

Provides access to the QAndroidJniEnvironment's JNIEnv pointer.