IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QJniEnvironment Class

The QJniEnvironment class provides access to the JNI Environment (JNIEnv).

This class was introduced in Qt 6.1.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QJniEnvironment Class

  • Header: QJniEnvironment

  • Since: Qt 6.1

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

Detailed Description

When using JNI, the JNIEnv class is a pointer to a function table and a member function for each JNI function that indirects through the table. JNIEnv provides most of the JNI functions. Every C++ native function receives a JNIEnv as the first argument. The JNI environment cannot be shared between threads.

Since JNIEnv doesn't do much error checking, such as exception checking and clearing, QJniEnvironment allows you to do that easily.

For more information about JNIEnv, see Java: Interface Function Table.

This API has been designed and tested for use with Android. It has not been tested for other platforms.

Member Type Documentation

 

enum QJniEnvironment::OutputMode

Constant

Value

Description

QJniEnvironment::OutputMode::Silent

0

The exceptions are cleaned silently

QJniEnvironment::OutputMode::Verbose

1

Prints the exceptions and their stack backtrace as an error to stderr stream.

Member Function Documentation

 

jclass QJniEnvironment::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 when using the default class loader.

Returns the class pointer or null if className is not found.

A use case for this function is searching for a class to call a JNI method that takes a jclass. This can be useful when doing multiple JNI calls on the same class object which can a bit faster than using a class name in each call. Additionally, this call looks for internally cached classes first before doing a JNI call, and returns such a class if found. The following code snippet creates an instance of the class CustomClass and then calls the printFromJava() method:

 
Sélectionnez
QJniEnvironment env;
jclass javaClass = env.findClass("org/qtproject/example/android/CustomClass");
QJniObject javaMessage = QJniObject::fromString("findClass example");
QJniObject::callStaticMethod<void>(javaClass, "printFromJava",
                                   "(Ljava/lang/String;)V", javaMessage.object<jstring>());

This call returns a global reference to the class object from the internally cached classes.

[since 6.2] jfieldID QJniEnvironment::findField(jclass clazz, const char *fieldName, const char *signature)

Searches for a member field of a class clazz. The field is specified by its fieldName and signature.

Returns the field ID or nullptr if the field is not found.

A usecase for this method is searching for class fields and caching their IDs, so that they could later be used for getting/setting the fields.

This function was introduced in Qt 6.2.

[since 6.2] jmethodID QJniEnvironment::findMethod(jclass clazz, const char *methodName, const char *signature)

Searches for an instance method of a class clazz. The method is specified by its methodName and signature.

Returns the method ID or nullptr if the method is not found.

A usecase for this method is searching for class methods and caching their IDs, so that they could later be used for calling the methods.

This function was introduced in Qt 6.2.

[since 6.2] jfieldID QJniEnvironment::findStaticField(jclass clazz, const char *fieldName, const char *signature)

Searches for a static field of a class clazz. The field is specified by its fieldName and signature.

Returns the field ID or nullptr if the field is not found.

A usecase for this method is searching for class fields and caching their IDs, so that they could later be used for getting/setting the fields.

This function was introduced in Qt 6.2.

[since 6.2] jmethodID QJniEnvironment::findStaticMethod(jclass clazz, const char *methodName, const char *signature)

Searches for a static method of a class clazz. The method is specified by its methodName and signature.

Returns the method ID or nullptr if the method is not found.

A usecase for this method is searching for class methods and caching their IDs, so that they could later be used for calling the methods.

 
Sélectionnez
QJniEnvironment env;
jclass javaClass = env.findClass("org/qtproject/example/android/CustomClass");
jmethodID methodId = env.findStaticMethod(javaClass,
                                          "staticJavaMethod",
                                          "(Ljava/lang/String;)V");
QJniObject javaMessage = QJniObject::fromString("findStaticMethod example");
QJniObject::callStaticMethod<void>(javaClass,
                                   methodId,
                                   javaMessage.object<jstring>());

This function was introduced in Qt 6.2.

[since 6.2] bool QJniEnvironment::isValid() const

Returns true if this instance holds a valid JNIEnv object.

This function was introduced in Qt 6.2.

bool QJniEnvironment::registerNativeMethods(const char *className, const JNINativeMethod[] methods, int size)

Registers the Java methods in the array methods of size size, each of which can call native C++ functions from class className. These methods must be registered before any attempt to call them.

Returns true if the registration is successful, otherwise false.

Each element in the methods array consists of:

  • The Java method name

  • Method signature

  • The C++ functions that will be executed

 
Sélectionnez
const JNINativeMethod methods[] =
                        {{"callNativeOne", "(I)V", reinterpret_cast<void *>(fromJavaOne)},
                        {"callNativeTwo", "(I)V", reinterpret_cast<void *>(fromJavaTwo)}};
QJniEnvironment env;
env.registerNativeMethods("org/qtproject/android/TestJavaClass", methods, 2);

bool QJniEnvironment::registerNativeMethods(jclass clazz, const JNINativeMethod[] methods, int size)

This is an overloaded function.

This overload uses a previously cached jclass instance clazz.

 
Sélectionnez
JNINativeMethod methods[] {{"callNativeOne", "(I)V", reinterpret_cast<void *>(fromJavaOne)},
                           {"callNativeTwo", "(I)V", reinterpret_cast<void *>(fromJavaTwo)}};
QJniEnvironment env;
jclass clazz = env.findClass("org/qtproject/android/TestJavaClass");
env.registerNativeMethods(clazz, methods, 2);

Obsolete Members for QJniEnvironment

The following members of class QJniEnvironment are deprecated. We strongly advise against using them in new code.

Obsolete Member Function Documentation

 
bool QJniEnvironment::registerNativeMethods(const char *className, JNINativeMethod[] methods, int size)

This function is deprecated since 6.2. We strongly advise against using it in new code.

This is an overloaded function.

Use the overload with a const JNINativeMethod[] instead.

Registers the Java methods in the array methods of size size, each of which can call native C++ functions from class className. These methods must be registered before any attempt to call them.

Returns true if the registration is successful, otherwise false.

Each element in the methods array consists of:

  • The Java method name

  • Method signature

  • The C++ functions that will be executed

 
Sélectionnez
JNINativeMethod methods[] = {{"callNativeOne", "(I)V", reinterpret_cast<void *>(fromJavaOne)},
                             {"callNativeTwo", "(I)V", reinterpret_cast<void *>(fromJavaTwo)}};
QJniEnvironment env;
env.registerNativeMethods("org/qtproject/android/TestJavaClass", methods, 2);

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+