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
QtWS25 Last Chance

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 881 Views
  • 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.
  • ismailI Offline
    ismailI Offline
    ismail
    wrote on 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.

    raven-worxR 1 Reply Last reply
    0
    • ismailI ismail

      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.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #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
      • ismailI Offline
        ismailI Offline
        ismail
        wrote on last edited by ismail
        #3

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

        1 Reply Last reply
        0

        • Login

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