Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Call Java method from c++
Forum Updated to NodeBB v4.3 + New Features

Call Java method from c++

Scheduled Pinned Locked Moved Solved Mobile and Embedded
3 Posts 2 Posters 749 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrdebug
    wrote on 29 Dec 2021, 18:33 last edited by mrdebug
    #1

    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?

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    K 1 Reply Last reply 30 Dec 2021, 09:51
    0
    • M mrdebug
      29 Dec 2021, 18:33

      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?

      K Offline
      K Offline
      KroMignon
      wrote on 30 Dec 2021, 09:51 last edited by KroMignon
      #2

      @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();
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mrdebug
        wrote on 31 Dec 2021, 09:28 last edited by mrdebug
        #3

        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();
                }
            }
        

        Need programmers to hire?
        www.labcsp.com
        www.denisgottardello.it
        GMT+1
        Skype: mrdebug

        1 Reply Last reply
        1

        1/3

        29 Dec 2021, 18:33

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved