Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Call Java method from c++



  • Hi I need to call from c++ this java method:

        public String GetScanResultAt(int count) {
            return "123";
        }
    

    I'm using this to call a java method without arguments that returs a string:

    qDebug() << QAndroidJniObject(QtAndroid::androidActivity().callObjectMethod<jstring>("GetScanResultAt")).toString();
    

    Which is the right way to change the line of code above to pass a int parameter?



  • @mrdebug said in Call Java method from c++:

    Which is the right way to change the line of code above to pass a int parameter?

    You have to add function signature:

    qDebug() << QAndroidJniObject(QtAndroid::androidActivity().callObjectMethod<jstring>("GetScanResultAt", "(I)Ljava/lang/String;", jint(55)).toString();
    


  • Below examples on how to use java methods with arguments

    Java

        public int Test01() {
            System.out.println("Test01");
            return 0;
        }
        public int Test02(int Value) {
            System.out.println("Test02");
            System.out.println(Value);
            return 0;
        }
        public int Test03(String Value) {
            System.out.println("Test03");
            System.out.println(Value);
            return 0;
        }
        public String Test04(int Value) {
            System.out.println("Test04");
            System.out.println(Value);
            return "Hello!!!";
        }
        public String Test04b(String Value) {
            System.out.println("Test04b");
            System.out.println(Value);
            return "Hello!!!";
        }
        public String[] Test05(String Value1, String Value2) {
            System.out.println("Test05");
            System.out.println(Value1);
            System.out.println(Value2);
            String[] Result= new String[2];
            Result[0]= "Hello";
            Result[1]= "world!!!";
            return Result;
        }
    

    Qt / C++

        qDebug() << "Test01 result:" << QtAndroid::androidActivity().callMethod<jint>("Test01");
        qDebug() << "Test02 result:" << QtAndroid::androidActivity().callMethod<jint>("Test02", "(I)I", 10);
    
    
        QAndroidJniObject Value1= QAndroidJniObject::fromString("Value1");
        QAndroidJniObject Value2= QAndroidJniObject::fromString("Value2");
    
        qDebug() << "Test03 result:" << QtAndroid::androidActivity().callMethod<jint>("Test03", "(Ljava/lang/String;)I", Value1.object<jstring>());
        qDebug() << "Test04 result:" << QAndroidJniObject(QtAndroid::androidActivity().callObjectMethod("Test04", "(I)Ljava/lang/String;", 123456)).toString();
        qDebug() << "Test04b result:" << QAndroidJniObject(QtAndroid::androidActivity().callObjectMethod("Test04b", "(Ljava/lang/String;)Ljava/lang/String;", Value1.object<jstring>())).toString();
    
        QAndroidJniObject stringArray= QtAndroid::androidActivity().callObjectMethod("Test05",
                                                                                    "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;",
                                                                                    Value1.object<jstring>(),
                                                                                    Value2.object<jstring>());
    
        if (stringArray.isValid()) {
            jobjectArray objectArray= stringArray.object<jobjectArray>();
            for (int count= 0; count< env->GetArrayLength(objectArray); count++) {
                QAndroidJniObject AndroidJniObject= env->GetObjectArrayElement(objectArray, count);
                qDebug() << AndroidJniObject.toString();
            }
        }
    

Log in to reply