Qt 调用 java代码,用于Android app的开发

Qt 调用java代码的一个示例,用于Android app的开发,需要在.pro文件中加入

QT += androidextras

Java使用JNI是为了使用C++的函数,而Qt这里使用JNI正好相反,是为了使用Java的函数

#include <QAndroidJniObject>
#include <QAndroidJniEnvironment>

    QAndroidJniEnvironment env;
    QAndroidJniObject str = QAndroidJniObject::fromString("Hello Qt on Android test");
    QAndroidJniObject separator = QAndroidJniObject::fromString(" ");
    QAndroidJniObject words = str.callObjectMethod("split",
                                         "(Ljava/lang/String;)[Ljava/lang/String;",
                                         separator.object<jstring>());
    jint len = str.callMethod<jint>("length");
    qDebug() << "--------------------------------------------" << len;

    jint size = env->GetArrayLength(words.object<jobjectArray>());
    QAndroidJniObject elem;
    for(int ii=0;ii<size;++ii){
        QAndroidJniObject elem = env->GetObjectArrayElement(words.object<jobjectArray>(), ii);
        qDebug() << "--------------------------------------------" << elem.toString();
    }

 输出:

D libHHT_armeabi-v7a.so: -------------------------------------------- 24
D libHHT_armeabi-v7a.so: -------------------------------------------- "Hello"
D libHHT_armeabi-v7a.so: -------------------------------------------- "Qt"
D libHHT_armeabi-v7a.so: -------------------------------------------- "on"
D libHHT_armeabi-v7a.so: -------------------------------------------- "Android"
D libHHT_armeabi-v7a.so: -------------------------------------------- "test"

posted on 2022-03-08 15:50  litandy  阅读(1269)  评论(0编辑  收藏  举报

导航