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

QAndroidJniObject Class

Provides APIs to call Java code from C++.

This class was introduced in Qt 5.2.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QAndroidJniObject Class

  • Header: QAndroidJniObject

  • Since: Qt 5.2

  • qmake: QT += androidextras

Detailed Description

 

General Notes

  • Class names needs to contain the fully-qualified class name, for example: "java/lang/String".

  • Method signatures are written as "(Arguments)ReturnType"

  • All object types are returned as a QAndroidJniObject.

Method Signatures

For functions that take no arguments, QAndroidJniObject provides convenience functions that will use the correct signature based on the provided template type. For example:

 
Sélectionnez
jint x = QAndroidJniObject::callMethod<jint>("getSize");
QAndroidJniObject::callMethod<void>("touch");

In other cases you will need to supply the signature yourself, and it is important that the signature matches the function you want to call. The signature structure is (A)R, where A is the type of the argument(s) and R is the return type. Array types in the signature must have the [ suffix and the fully-qualified type names must have the L prefix and ; suffix.

The example below demonstrates how to call two different static functions.

 
Sélectionnez
// Java class
package org.qtproject.qt5;
class TestClass
{
   static String fromNumber(int x) { ... }
   static String[] stringArray(String s1, String s2) { ... }
}

The signature for the first function is "(I)Ljava/lang/String;"

 
Sélectionnez
// C++ code
QAndroidJniObject stringNumber = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
                                                                           "fromNumber"
                                                                           "(I)Ljava/lang/String;",
                                                                           10);

and the signature for the second function is "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"

 
Sélectionnez
// C++ code
QAndroidJniObject string1 = QAndroidJniObject::fromString("String1");
QAndroidJniObject string2 = QAndroidJniObject::fromString("String2");
QAndroidJniObject stringArray = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
                                                                          "stringArray"
                                                                          "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"
                                                                           string1.object<jstring>(),
                                                                           string2.object<jstring>());

Handling Java Exception

When calling Java functions that might throw an exception, it is important that you check, handle and clear out the exception before continuing.

It is unsafe to make a JNI call when there are exceptions pending.

 
Sélectionnez
void functionException()
{
    QAndroidJniObject myString = QAndroidJniObject::fromString("Hello");
    jchar c = myString.callMethod<jchar>("charAt", "(I)C", 1000);
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck()) {
        // Handle exception here.
        env->ExceptionClear();
    }
}

Java Native Methods

Java native methods makes it possible to call native code from Java, this is done by creating a function declaration in Java and prefixing it with the native keyword. Before a native function can be called from Java, you need to map the Java native function to a native function in your code. Mapping functions can be done by calling the RegisterNatives() function through the JNI environment pointer.

The example below demonstrates how this could be done.

Java implementation:

 
Sélectionnez
class FooJavaClass
{
    public static void foo(int x)
    {
        if (x < 100)
            callNativeOne(x);
        else
            callNativeTwo(x);
    }

private static native void callNativeOne(int x);
private static native void callNativeTwo(int x);

}

C++ Implementation:

 
Sélectionnez
static void fromJavaOne(JNIEnv *env, jobject thiz, jint x)
{
    Q_UNUSED(env)
    Q_UNUSED(thiz)
    qDebug() << x << "< 100";
}

static void fromJavaTwo(JNIEnv *env, jobject thiz, jint x)
{
    Q_UNUSED(env)
    Q_UNUSED(thiz)
    qDebug() << x << ">= 100";
}

void registerNativeMethods() {
    JNINativeMethod methods[] {{"callNativeOne", "(I)V", reinterpret_cast<void *>(fromJavaOne)},
                               {"callNativeTwo", "(I)V", reinterpret_cast<void *>(fromJavaTwo)}};

    QAndroidJniObject javaClass("my/java/project/FooJavaClass");
    QAndroidJniEnvironment env;
    jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
    env->RegisterNatives(objectClass,
                         methods,
                         sizeof(methods) / sizeof(methods[0]));
    env->DeleteLocalRef(objectClass);
}

void foo()
{
    QAndroidJniObject::callStaticMethod<void>("my/java/project/FooJavaClass", "foo", "(I)V", 10);  // Output: 10 < 100
    QAndroidJniObject::callStaticMethod<void>("my/java/project/FooJavaClass", "foo", "(I)V", 100); // Output: 100 >= 100
}

The Lifetime of a Java Object

Most objects received from Java will be local references and will only stay valid in the scope you received them. After that, the object becomes eligible for garbage collection. If you want to keep a Java object alive you need to either create a new global reference to the object and release it when you are done, or construct a new QAndroidJniObject and let it manage the lifetime of the Java object.

The QAndroidJniObject does only manage its own references, if you construct a QAndroidJniObject from a global or local reference that reference will not be released by the QAndroidJniObject.

JNI Types

 
Object Types

Type

Signature

jobject

Ljava/lang/Object;

jclass

Ljava/lang/Class;

jstring

Ljava/lang/String;

jthrowable

Ljava/lang/Throwable;

jobjectArray

[Ljava/lang/Object;

jarray

[<type>

jbooleanArray

[Z

jbyteArray

[B

jcharArray

[C

jshortArray

[S

jintArray

[I

jlongArray

[J

jfloatArray

[F

jdoubleArray

[D

Primitive Types

Type

Signature

jboolean

Z

jbyte

B

jchar

C

jshort

S

jint

I

jlong

J

jfloat

F

jdouble

D

Other

Type

Signature

void

V

Custom type

L<fully-qualified-name>;

For more information about JNI see: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html

See Also

Member Function Documentation

 

QAndroidJniObject::QAndroidJniObject()

Constructs an invalid QAndroidJniObject.

See Also

See also isValid()

[explicit] QAndroidJniObject::QAndroidJniObject(const char *className)

Constructs a new QAndroidJniObject by calling the default constructor of className.

 
Sélectionnez
...
QAndroidJniObject myJavaString("java/lang/String");
...

[explicit] QAndroidJniObject::QAndroidJniObject(const char *className, const char *signature, ...)

Constructs a new QAndroidJniObject by calling the constructor of className with signature and arguments.

 
Sélectionnez
...
jstring myJStringArg = ...;
QAndroidJniObject myNewJavaString("java/lang/String", "(Ljava/lang/String;)V", myJStringArg);
...

[explicit] QAndroidJniObject::QAndroidJniObject(jclass clazz)

Constructs a new QAndroidJniObject by calling the default constructor of clazz.

Note: The QAndroidJniObject will create a new reference to the class clazz and releases it again when it is destroyed. References to the class created outside the QAndroidJniObject needs to be managed by the caller.

[explicit] QAndroidJniObject::QAndroidJniObject(jclass clazz, const char *signature, ...)

Constructs a new QAndroidJniObject from clazz by calling the constructor with signature and arguments.

 
Sélectionnez
jclass myClazz = ...;
QAndroidJniObject::QAndroidJniObject(myClazz, "(I)V", 3);

QAndroidJniObject::QAndroidJniObject(int object)

Constructs a new QAndroidJniObject around the Java object object.

Note: The QAndroidJniObject will hold a reference to the Java object object and release it when destroyed. Any references to the Java object object outside QAndroidJniObject needs to be managed by the caller.

See Also

See also fromLocalRef()

QAndroidJniObject::~QAndroidJniObject()

Destroys the QAndroidJniObject and releases any references held by the QAndroidJniObject.

T QAndroidJniObject::callMethod(const char *methodName) const

Calls the method methodName and returns the value.

 
Sélectionnez
QAndroidJniObject myJavaString = ...;
jint size = myJavaString.callMethod&lt;jint&gt;("length");

T QAndroidJniObject::callMethod(const char *methodName, const char *sig, ...) const

Calls the method methodName with a signature sig and returns the value.

 
Sélectionnez
QAndroidJniObject myJavaString = ...;
jint index = myJavaString.callMethod&lt;jint&gt;("indexOf", "(I)I", 0x0051);

QAndroidJniObject QAndroidJniObject::callObjectMethod(const char *methodName) const

Calls the Java objects method methodName and returns a new QAndroidJniObject for the returned Java object.

 
Sélectionnez
...
QAndroidJniObject myJavaString1 = ...;
QAndroidJniObject myJavaString2 = myJavaString1.callObjectMethod&lt;jstring&gt;("toString");
...

QAndroidJniObject QAndroidJniObject::callObjectMethod(const char *methodName, const char *signature, ...) const

Calls the Java object's method methodName with the signature signature and arguments

 
Sélectionnez
QAndroidJniObject myJavaString; ==&gt; "Hello, Java"
QAndroidJniObject mySubstring = myJavaString.callObjectMethod("substring", "(II)Ljava/lang/String;", 7, 10);

[static] T QAndroidJniObject::callStaticMethod(const char *className, const char *methodName)

Calls the static method methodName on class className and returns the value.

 
Sélectionnez
jint value = QAndroidJniObject::callStaticMethod&lt;jint&gt;("MyClass", "staticMethod");

[static] T QAndroidJniObject::callStaticMethod(const char *className, const char *methodName, const char *signature, ...)

Calls the static method with methodName with signature on class className with optional arguments.

 
Sélectionnez
...
jint a = 2;
jint b = 4;
jint max = QAndroidJniObject::callStaticMethod&lt;jint&gt;("java/lang/Math", "max", "(II)I", a, b);
...

[static] T QAndroidJniObject::callStaticMethod(jclass clazz, const char *methodName)

Calls the static method methodName on clazz and returns the value.

 
Sélectionnez
...
jclass javaMathClass = ...; // ("java/lang/Math")
jdouble randNr = QAndroidJniObject::callStaticMethod&lt;jdouble&gt;(javaMathClass, "random");
...

[static] T QAndroidJniObject::callStaticMethod(jclass clazz, const char *methodName, const char *signature, ...)

Calls the static method methodName with signature on clazz and returns the value.

 
Sélectionnez
...
jclass javaMathClass = ...; // ("java/lang/Math")
jint a = 2;
jint b = 4;
jint max = QAndroidJniObject::callStaticMethod&lt;jint&gt;(javaMathClass, "max", "(II)I", a, b);
...

[static] QAndroidJniObject QAndroidJniObject::callStaticObjectMethod(const char *className, const char *methodName)

Calls the static method with methodName on the class className.

 
Sélectionnez
QAndroidJniObject string = QAndroidJniObject::callStaticObjectMethod&lt;jstring&gt;("CustomClass", "getClassName");

[static] QAndroidJniObject QAndroidJniObject::callStaticObjectMethod(const char *className, const char *methodName, const char *signature, ...)

Calls the static method with methodName and signature on the class className.

 
Sélectionnez
QAndroidJniObject thread = QAndroidJniObject::callStaticObjectMethod("java/lang/Thread", "currentThread", "()Ljava/lang/Thread;");
QAndroidJniObject string = QAndroidJniObject::callStaticObjectMethod("java/lang/String", "valueOf", "(I)Ljava/lang/String;", 10);

[static] QAndroidJniObject QAndroidJniObject::callStaticObjectMethod(jclass clazz, const char *methodName)

Calls the static method with methodName on clazz.

[static] QAndroidJniObject QAndroidJniObject::callStaticObjectMethod(jclass clazz, const char *methodName, const char *signature, ...)

Calls the static method with methodName and signature on class clazz.

[static, since 5.7] QAndroidJniObject QAndroidJniObject::fromLocalRef(int localRef)

Creates a QAndroidJniObject from the local JNI reference localRef. This function takes ownership of localRef and frees it before returning.

Only call this function with a local JNI reference. For example, most raw JNI calls, through the JNI environment, returns local references to a java object.

 
Sélectionnez
jobject localRef = env-&gt;GetObjectArrayElement(array, index);
QAndroidJniObject element = QAndroidJniObject::fromLocalRef(localRef);

This function was introduced in Qt 5.7.

[static] QAndroidJniObject QAndroidJniObject::fromString(const QString &string)

Creates a Java string from the QString string and returns a QAndroidJniObject holding that string.

 
Sélectionnez
...
QString myQString = "QString";
QAndroidJniObject myJavaString = QAndroidJniObject::fromString(myQString);
...
See Also

See also toString()

T QAndroidJniObject::getField(const char *fieldName) const

Retrieves the value of the field fieldName.

 
Sélectionnez
QAndroidJniObject volumeControl = ...;
jint fieldValue = volumeControl.getField&lt;jint&gt;("MAX_VOLUME");

QAndroidJniObject QAndroidJniObject::getObjectField(const char *fieldName) const

Retrieves the object of field fieldName.

 
Sélectionnez
QAndroidJniObject field = jniObject.getObjectField&lt;jstring&gt;("FIELD_NAME");

QAndroidJniObject QAndroidJniObject::getObjectField(const char *fieldName, const char *signature) const

Retrieves the object from the field with signature and fieldName.

Since Qt 5.3 this function can be used without a template type.

 
Sélectionnez
QAndroidJniObject field = jniObject.getObjectField("FIELD_NAME", "Ljava/lang/String;");

[static] T QAndroidJniObject::getStaticField(const char *className, const char *fieldName)

Retrieves the value from the static field fieldName on the class className.

[static] T QAndroidJniObject::getStaticField(jclass clazz, const char *fieldName)

Retrieves the value from the static field fieldName on clazz.

[static] QAndroidJniObject QAndroidJniObject::getStaticObjectField(const char *className, const char *fieldName)

Retrieves the object from the field fieldName on the class className.

 
Sélectionnez
QAndroidJniObject jobj = QAndroidJniObject::getStaticObjectField&lt;jstring&gt;("class/with/Fields", "FIELD_NAME");

[static] QAndroidJniObject QAndroidJniObject::getStaticObjectField(const char *className, const char *fieldName, const char *signature)

Retrieves the object from the field with signature and fieldName on class className.

Since Qt 5.3 this function can be used without a template type.

 
Sélectionnez
QAndroidJniObject jobj = QAndroidJniObject::getStaticObjectField("class/with/Fields", "FIELD_NAME", "Ljava/lang/String;");

[static] QAndroidJniObject QAndroidJniObject::getStaticObjectField(jclass clazz, const char *fieldName)

Retrieves the object from the field fieldName on clazz.

 
Sélectionnez
QAndroidJniObject jobj = QAndroidJniObject::getStaticObjectField&lt;jstring&gt;(clazz, "FIELD_NAME");

[static] QAndroidJniObject QAndroidJniObject::getStaticObjectField(jclass clazz, const char *fieldName, const char *signature)

Retrieves the object from the field with signature and fieldName on clazz.

Since Qt 5.3 this function can be used without a template type.

 
Sélectionnez
QAndroidJniObject jobj = QAndroidJniObject::getStaticObjectField(clazz, "FIELD_NAME", "Ljava/lang/String;");

[static] bool QAndroidJniObject::isClassAvailable(const char *className)

Returns true if the Java class className is available.

 
Sélectionnez
...
if (QAndroidJniObject::isClassAvailable("java/lang/String")) {
   ...
}
...

bool QAndroidJniObject::isValid() const

Returns true if this instance holds a valid Java object.

 
Sélectionnez
...
QAndroidJniObject qjniObject;                        ==&gt; isValid() == false
QAndroidJniObject qjniObject(0)                      ==&gt; isValid() == false
QAndroidJniObject qjniObject("could/not/find/Class") ==&gt; isValid() == false
...

T QAndroidJniObject::object() const

Returns the object held by the QAndroidJniObject as type T.

 
Sélectionnez
QAndroidJniObject string = QAndroidJniObject::fromString("Hello, JNI");
jstring jstring = string.object&lt;jstring&gt;();

The returned object is still owned by the QAndroidJniObject. If you want to keep the object valid you should create a new QAndroidJniObject or make a new global reference to the object and free it yourself.

 
Sélectionnez
void functionScope()
{
    QString helloString("Hello");
    jstring myJString = 0;
    {
        QAndroidJniObject string = QAndroidJniObject::fromString(helloString);
        myJString = string.object&lt;jstring&gt;();
    }

   // Ops! myJString is no longer valid.
}

Since Qt 5.3 this function can be used without a template type, if the returned type is a jobject.

 
Sélectionnez
jobject object = jniObject.object();

void QAndroidJniObject::setField(const char *fieldName, T value)

Sets the value of fieldName to value.

 
Sélectionnez
...
QAndroidJniObject obj;
obj.setField&lt;jint&gt;("AN_INT_FIELD", 10);
jstring myString = ...
obj.setField&lt;jstring&gt;("A_STRING_FIELD", myString);
...

void QAndroidJniObject::setField(const char *fieldName, const char *signature, T value)

Sets the value of fieldName with signature to value.

 
Sélectionnez
QAndroidJniObject stringArray = ...;
QAndroidJniObject obj = ...;
obj.setField&lt;jobjectArray&gt;("KEY_VALUES", "([Ljava/lang/String;)V", stringArray.object&lt;jobjectArray&gt;())

[static] void QAndroidJniObject::setStaticField(const char *className, const char *fieldName, const char *signature, T value)

Sets the static field with fieldName and signature to value on class className.

[static] void QAndroidJniObject::setStaticField(const char *className, const char *fieldName, T value)

Sets the value of the static field fieldName in class className to value.

[static] void QAndroidJniObject::setStaticField(jclass clazz, const char *fieldName, const char *signature, T value)

Sets the static field with fieldName and signature to value on class clazz.

[static] void QAndroidJniObject::setStaticField(jclass clazz, const char *fieldName, T value)

Sets the static field fieldName of the class clazz to value.

QString QAndroidJniObject::toString() const

Returns a QString with a string representation of the java object. Calling this function on a Java String object is a convenient way of getting the actual string data.

 
Sélectionnez
QAndroidJniObject string = ...; //  "Hello Java"
QString qstring = string.toString(); // "Hello Java"
See Also

See also fromString()

QAndroidJniObject &QAndroidJniObject::operator=(T object)

Replace the current object with object. The old Java object will be released.

Related Non-Members

 

bool operator!=(const QAndroidJniObject &o1, const QAndroidJniObject &o2)

Returns true if o1 holds a reference to a different object then o2.

bool operator==(const QAndroidJniObject &o1, const QAndroidJniObject &o2)

Returns true if both objects, o1 and o2, are referencing the same Java object, or if both are NULL. In any other cases false will be returned.

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