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. Android service java method is returning always same value to Qt Android service
Forum Updated to NodeBB v4.3 + New Features

Android service java method is returning always same value to Qt Android service

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
androidservicejava jni
3 Posts 2 Posters 901 Views 2 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.
  • I Offline
    I Offline
    ismail
    wrote on 9 Feb 2019, 11:56 last edited by
    #1

    I'm trying to return value to Qt service from java service code. Java always returns initial value for variable. However when I run same code in Qt main activity, it is working.

    Code in BackService java class:

    public static int SAYK = 0;
    
    public static void callNorm(int valSy) {
        BackService.SAYK = valSy;
        System.out.println("Result= "+BackService.SAYK);
    }
    
    public static int retStt() {
        return BackService.SAYK;
    }
    

    C++ code in Qt Service:

    int jobj = QAndroidJniObject::callStaticMethod<jint>("org/qtproject/example_v2/BackService",
                                                         "retStt",
                                                         "()I");
    

    When I call "callNorm" function from java, system prints new value of SAYK. But when I call "retStt" from Qt service, it returns only initial value 0. I tried also with callStaticObjectMethod, getStaticField, java native functions(gives unsatisfied link error) etc. Unfortunately, none of these worked. Also I can call java functions that has not return value from Qt Service, without problems.

    R 1 Reply Last reply 9 Feb 2019, 18:24
    0
    • I ismail
      9 Feb 2019, 11:56

      I'm trying to return value to Qt service from java service code. Java always returns initial value for variable. However when I run same code in Qt main activity, it is working.

      Code in BackService java class:

      public static int SAYK = 0;
      
      public static void callNorm(int valSy) {
          BackService.SAYK = valSy;
          System.out.println("Result= "+BackService.SAYK);
      }
      
      public static int retStt() {
          return BackService.SAYK;
      }
      

      C++ code in Qt Service:

      int jobj = QAndroidJniObject::callStaticMethod<jint>("org/qtproject/example_v2/BackService",
                                                           "retStt",
                                                           "()I");
      

      When I call "callNorm" function from java, system prints new value of SAYK. But when I call "retStt" from Qt service, it returns only initial value 0. I tried also with callStaticObjectMethod, getStaticField, java native functions(gives unsatisfied link error) etc. Unfortunately, none of these worked. Also I can call java functions that has not return value from Qt Service, without problems.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 9 Feb 2019, 18:24 last edited by raven-worx 2 Sept 2019, 19:02
      #2

      @ismail
      IIRC since you calling from Qt (which runs in a separate thread) you get separate static variables other than the java thread.

      Try if that works for you:

      jint jobj;
      QtAndroid::runOnAndroidThreadSync([&jobj](){
          jobj = QAndroidJniObject::callStaticMethod<jint>("org/qtproject/example_v2/BackService",
                                                           "retStt",
                                                           "()I");
      });
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ismail
        wrote on 10 Feb 2019, 11:55 last edited by ismail
        #3

        Unfortunately it didn't work. I still get the same value. Any other suggestion?

        1 Reply Last reply
        0

        3/3

        10 Feb 2019, 11:55

        • Login

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