QJniEnvironment 类提供对 JNIEnv (JNI 环境) 的访问。 更多...
头: | #include <QJniEnvironment> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Since: | Qt 6.1 |
enum class | OutputMode { Silent, Verbose } |
QJniEnvironment () | |
~QJniEnvironment () | |
bool | checkAndClearExceptions (QJniEnvironment::OutputMode outputMode = OutputMode::Verbose) |
jclass | findClass (const char * className ) |
jfieldID | findField (jclass clazz , const char * fieldName , const char * signature ) |
jfieldID | findField (jclass clazz , const char * fieldName ) |
jmethodID | findMethod (jclass clazz , const char * methodName , const char * signature ) |
jmethodID | findMethod (jclass clazz , const char * methodName ) |
jfieldID | findStaticField (jclass clazz , const char * fieldName , const char * signature ) |
jfieldID | findStaticField (jclass clazz , const char * fieldName ) |
jmethodID | findStaticMethod (jclass clazz , const char * methodName , const char * signature ) |
jmethodID | findStaticMethod (jclass clazz , const char * methodName ) |
bool | isValid () const |
JNIEnv * | jniEnv () const |
bool | registerNativeMethods (const char * className , const JNINativeMethod [] methods , int size ) |
bool | registerNativeMethods (jclass clazz , const JNINativeMethod [] methods , int size ) |
JNIEnv & | operator* () const |
JNIEnv * | operator-> () const |
bool | checkAndClearExceptions (JNIEnv * env , QJniEnvironment::OutputMode outputMode = OutputMode::Verbose) |
JavaVM * | javaVM () |
当使用 JNI 时,
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.
由于
JNIEnv
doesn't do much error checking, such as exception checking and clearing, QJniEnvironment allows you to do that easily.
JNIEnv 的更多有关信息,见 Java:接口功能表 .
注意: This API has been designed and tested for use with Android. It has not been tested for other platforms.
常量 | 值 | 描述 |
---|---|---|
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.
|
Constructs a new JNI Environment object and attaches the current thread to the Java VM.
分离当前线程从 Java VM 和销毁 QJniEnvironment object. This will clear any pending exception by calling checkAndClearExceptions ().
Cleans any pending exceptions either silently or reporting stack backtrace, depending on the outputMode .
In contrast to
QJniObject
, which handles exceptions internally, if you make JNI calls directly via
JNIEnv
, you need to clear any potential exceptions after the call using this function. For more information about
JNIEnv
calls that can throw an exception, see
JNI Functions
.
返回
true
when a pending exception was cleared.
[static]
bool
QJniEnvironment::
checkAndClearExceptions
(
JNIEnv
*
env
,
QJniEnvironment::OutputMode
outputMode
= OutputMode::Verbose)
Cleans any pending exceptions for
env
, either silently or reporting stack backtrace, depending on the
outputMode
. This is useful when you already have a
JNIEnv
pointer such as in a native function implementation.
In contrast to
QJniObject
, which handles exceptions internally, if you make JNI calls directly via
JNIEnv
, you need to clear any potential exceptions after the call using this function. For more information about
JNIEnv
calls that can throw an exception, see
JNI Functions
.
返回
true
when a pending exception was cleared.
搜索 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 找不到。
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()
方法:
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.
该函数在 Qt 6.2 引入。
[since 6.4]
template <typename T>
jfieldID
QJniEnvironment::
findField
(
jclass
clazz
, const
char
*
fieldName
)
Searches for a member field of a class clazz . The field is specified by its fieldName . The signature of the field is deduced from the template parameter.
Returns the field ID or
nullptr
if the field is not found.
该函数在 Qt 6.4 引入。
[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.
该函数在 Qt 6.2 引入。
[since 6.4]
template <typename Args>
jmethodID
QJniEnvironment::
findMethod
(
jclass
clazz
, const
char
*
methodName
)
Searches for an instance method of a class clazz . The method is specified by its methodName , the signature is deduced from the template parameters.
Returns the method ID or
nullptr
if the method is not found.
该函数在 Qt 6.4 引入。
[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.
该函数在 Qt 6.2 引入。
[since 6.4]
template <typename T>
jfieldID
QJniEnvironment::
findStaticField
(
jclass
clazz
, const
char
*
fieldName
)
Searches for a static field of a class clazz . The field is specified by its fieldName . The signature of the field is deduced from the template parameter.
Returns the field ID or
nullptr
if the field is not found.
该函数在 Qt 6.4 引入。
[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.
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>());
该函数在 Qt 6.2 引入。
[since 6.4]
template <typename Args>
jmethodID
QJniEnvironment::
findStaticMethod
(
jclass
clazz
, const
char
*
methodName
)
Searches for an instance method of a class clazz . The method is specified by its methodName , the signature is deduced from the template parameters.
Returns the method ID or
nullptr
if the method is not found.
QJniEnvironment env; jclass javaClass = env.findClass("org/qtproject/example/android/CustomClass"); jmethodID methodId = env.findStaticMethod<void, jstring>(javaClass, "staticJavaMethod"); QJniObject javaMessage = QJniObject::fromString("findStaticMethod example"); QJniObject::callStaticMethod<void>(javaClass, methodId, javaMessage.object<jstring>());
该函数在 Qt 6.4 引入。
[since 6.2]
bool
QJniEnvironment::
isValid
() const
返回
true
if this instance holds a valid JNIEnv object.
该函数在 Qt 6.2 引入。
[static]
JavaVM
*QJniEnvironment::
javaVM
()
Returns the Java VM interface for the current process. Although it might be possible to have multiple Java VMs per process, Android allows only one.
Returns the JNI Environment's
JNIEnv
指针。
Registers the Java methods in the array methods 的大小 size , each of which can call native C++ functions from class className . These methods must be registered before any attempt to call them.
返回
true
if the registration is successful, otherwise
false
.
Each element in the methods array consists of:
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);
这是重载函数。
This overload uses a previously cached jclass instance clazz .
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);
Returns the JNI Environment's
JNIEnv
对象。
Provides access to the JNI Environment's
JNIEnv
指针。