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. Qt Android get phone number
Forum Update on Monday, May 27th 2025

Qt Android get phone number

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
10 Posts 4 Posters 978 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.
  • C Offline
    C Offline
    Creatorczyk
    wrote on 12 Apr 2022, 12:17 last edited by Creatorczyk 4 Dec 2022, 13:32
    #1

    Hi,
    In my mobile app I want get phone number. So I added uses permisions:

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
    

    And I created class PhoneInfo:

    package JavaApi;
    
    import android.content.Context;
    import android.telephony.TelephonyManager;
    
    public class PhoneInfo {
        public String getHelloJava() {
                return "Hello java";
            }
    
        public String getPhoneNumber(){
                TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                String mPhoneNumber = tMgr.getLine1Number();
                return mPhoneNumber;       
           }
    }
    

    I try use class by my c++ code:

    if(QAndroidJniObject::isClassAvailable("JavaApi/PhoneInfo"))
        {
            QAndroidJniObject someJavaObject = QAndroidJniObject("JavaApi/PhoneInfo");
            qDebug() << someJavaObject.callObjectMethod<jstring>("getHelloJava").toString();
            qDebug() << someJavaObject.callObjectMethod<jstring>("getPhoneNumber").toString();
        }
    

    I tested earlier class "getHelloJava" and it works fine. Now I want use class "getPhone number" but unfortunately I got compile error java file:

    :-1: error: /media/sf_GitLab/app/build/android-build/src/PhoneInfo.java:12: error: cannot find symbol
    
    
    :-1: error:             TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                                                          ^
      symbol:   method getSystemService(String)
      location: class PhoneInfo
    1 error
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':compileDebugJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
    * Get more help at https://help.gradle.org
    
    BUILD FAILED in 1s
    
    
    :-1: error: Building the android package failed!
      -- For more information, run this command with --verbose.
    
    

    How could I fix it? I dont programing java everyday.

    J J K 3 Replies Last reply 12 Apr 2022, 12:34
    0
    • C Creatorczyk
      12 Apr 2022, 12:17

      Hi,
      In my mobile app I want get phone number. So I added uses permisions:

      <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
      <uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
      

      And I created class PhoneInfo:

      package JavaApi;
      
      import android.content.Context;
      import android.telephony.TelephonyManager;
      
      public class PhoneInfo {
          public String getHelloJava() {
                  return "Hello java";
              }
      
          public String getPhoneNumber(){
                  TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                  String mPhoneNumber = tMgr.getLine1Number();
                  return mPhoneNumber;       
             }
      }
      

      I try use class by my c++ code:

      if(QAndroidJniObject::isClassAvailable("JavaApi/PhoneInfo"))
          {
              QAndroidJniObject someJavaObject = QAndroidJniObject("JavaApi/PhoneInfo");
              qDebug() << someJavaObject.callObjectMethod<jstring>("getHelloJava").toString();
              qDebug() << someJavaObject.callObjectMethod<jstring>("getPhoneNumber").toString();
          }
      

      I tested earlier class "getHelloJava" and it works fine. Now I want use class "getPhone number" but unfortunately I got compile error java file:

      :-1: error: /media/sf_GitLab/app/build/android-build/src/PhoneInfo.java:12: error: cannot find symbol
      
      
      :-1: error:             TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                                                            ^
        symbol:   method getSystemService(String)
        location: class PhoneInfo
      1 error
      
      FAILURE: Build failed with an exception.
      
      * What went wrong:
      Execution failed for task ':compileDebugJavaWithJavac'.
      > Compilation failed; see the compiler error output for details.
      
      * Try:
      Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
      
      * Get more help at https://help.gradle.org
      
      BUILD FAILED in 1s
      
      
      :-1: error: Building the android package failed!
        -- For more information, run this command with --verbose.
      
      

      How could I fix it? I dont programing java everyday.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 12 Apr 2022, 12:34 last edited by
      #2

      @Creatorczyk said in Qt Android get phone number:

      return PhoneNumber;

      There is no such variable in that class.
      I guess you mean mPhoneNumber?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply 12 Apr 2022, 12:38
      0
      • J jsulm
        12 Apr 2022, 12:34

        @Creatorczyk said in Qt Android get phone number:

        return PhoneNumber;

        There is no such variable in that class.
        I guess you mean mPhoneNumber?

        C Offline
        C Offline
        Creatorczyk
        wrote on 12 Apr 2022, 12:38 last edited by
        #3

        @jsulm Sorry Its error from copy/paste text to this post. I corrected it. I cant still build project

        J 1 Reply Last reply 12 Apr 2022, 12:48
        0
        • C Creatorczyk
          12 Apr 2022, 12:38

          @jsulm Sorry Its error from copy/paste text to this post. I corrected it. I cant still build project

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 12 Apr 2022, 12:48 last edited by
          #4

          @Creatorczyk But error message is still the same: cannot find symbol
          return PhoneNumber;

          Maybe you need to completely rebuild?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Creatorczyk
            12 Apr 2022, 12:17

            Hi,
            In my mobile app I want get phone number. So I added uses permisions:

            <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
            <uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
            

            And I created class PhoneInfo:

            package JavaApi;
            
            import android.content.Context;
            import android.telephony.TelephonyManager;
            
            public class PhoneInfo {
                public String getHelloJava() {
                        return "Hello java";
                    }
            
                public String getPhoneNumber(){
                        TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                        String mPhoneNumber = tMgr.getLine1Number();
                        return mPhoneNumber;       
                   }
            }
            

            I try use class by my c++ code:

            if(QAndroidJniObject::isClassAvailable("JavaApi/PhoneInfo"))
                {
                    QAndroidJniObject someJavaObject = QAndroidJniObject("JavaApi/PhoneInfo");
                    qDebug() << someJavaObject.callObjectMethod<jstring>("getHelloJava").toString();
                    qDebug() << someJavaObject.callObjectMethod<jstring>("getPhoneNumber").toString();
                }
            

            I tested earlier class "getHelloJava" and it works fine. Now I want use class "getPhone number" but unfortunately I got compile error java file:

            :-1: error: /media/sf_GitLab/app/build/android-build/src/PhoneInfo.java:12: error: cannot find symbol
            
            
            :-1: error:             TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                                                                  ^
              symbol:   method getSystemService(String)
              location: class PhoneInfo
            1 error
            
            FAILURE: Build failed with an exception.
            
            * What went wrong:
            Execution failed for task ':compileDebugJavaWithJavac'.
            > Compilation failed; see the compiler error output for details.
            
            * Try:
            Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
            
            * Get more help at https://help.gradle.org
            
            BUILD FAILED in 1s
            
            
            :-1: error: Building the android package failed!
              -- For more information, run this command with --verbose.
            
            

            How could I fix it? I dont programing java everyday.

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 12 Apr 2022, 13:12 last edited by
            #5

            @Creatorczyk
            I'm by no means an expert in Java, but when I try to access/call getSystemService() I do that on an activity instance. Since your PhoneInfo class does not extend QtActivity try it with

            QtNative.activity().getSystemService(...);


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • C Creatorczyk
              12 Apr 2022, 12:17

              Hi,
              In my mobile app I want get phone number. So I added uses permisions:

              <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
              <uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
              

              And I created class PhoneInfo:

              package JavaApi;
              
              import android.content.Context;
              import android.telephony.TelephonyManager;
              
              public class PhoneInfo {
                  public String getHelloJava() {
                          return "Hello java";
                      }
              
                  public String getPhoneNumber(){
                          TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                          String mPhoneNumber = tMgr.getLine1Number();
                          return mPhoneNumber;       
                     }
              }
              

              I try use class by my c++ code:

              if(QAndroidJniObject::isClassAvailable("JavaApi/PhoneInfo"))
                  {
                      QAndroidJniObject someJavaObject = QAndroidJniObject("JavaApi/PhoneInfo");
                      qDebug() << someJavaObject.callObjectMethod<jstring>("getHelloJava").toString();
                      qDebug() << someJavaObject.callObjectMethod<jstring>("getPhoneNumber").toString();
                  }
              

              I tested earlier class "getHelloJava" and it works fine. Now I want use class "getPhone number" but unfortunately I got compile error java file:

              :-1: error: /media/sf_GitLab/app/build/android-build/src/PhoneInfo.java:12: error: cannot find symbol
              
              
              :-1: error:             TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                                                                    ^
                symbol:   method getSystemService(String)
                location: class PhoneInfo
              1 error
              
              FAILURE: Build failed with an exception.
              
              * What went wrong:
              Execution failed for task ':compileDebugJavaWithJavac'.
              > Compilation failed; see the compiler error output for details.
              
              * Try:
              Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
              
              * Get more help at https://help.gradle.org
              
              BUILD FAILED in 1s
              
              
              :-1: error: Building the android package failed!
                -- For more information, run this command with --verbose.
              
              

              How could I fix it? I dont programing java everyday.

              K Offline
              K Offline
              KroMignon
              wrote on 12 Apr 2022, 13:14 last edited by KroMignon 4 Dec 2022, 13:16
              #6

              @Creatorczyk said in Qt Android get phone number:

              TelephonyManager tMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
              

              This cannot work! getSystemService() is a member of android.content.Context

              You could try this:

              Package JavaApi;
              
              import android.content.Context;
              import android.telephony.TelephonyManager;
              
              public class PhoneInfo {
                  public String getHelloJava() {
                          return "Hello java";
                      }
              
                  public String getPhoneNumber(Context context){
                          TelephonyManager tMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                          String mPhoneNumber = tMgr.getLine1Number();
                          return mPhoneNumber;
                      }
              }
              

              And, on C++ side:

              if(QAndroidJniObject::isClassAvailable("JavaApi/PhoneInfo"))
                  {
                      QAndroidJniObject someJavaObject = QAndroidJniObject("JavaApi/PhoneInfo");
                      qDebug() << someJavaObject.callObjectMethod<jstring>("getHelloJava").toString();
                      qDebug() << someJavaObject.callObjectMethod<jstring>(
                                                      "getPhoneNumber", 
                                                      "(Landroid/content/Context;)Ljava/lang/String;",
                                                      QtAndroid::androidContext()
                                                       ).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)

              C 1 Reply Last reply 12 Apr 2022, 13:45
              1
              • K KroMignon
                12 Apr 2022, 13:14

                @Creatorczyk said in Qt Android get phone number:

                TelephonyManager tMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
                

                This cannot work! getSystemService() is a member of android.content.Context

                You could try this:

                Package JavaApi;
                
                import android.content.Context;
                import android.telephony.TelephonyManager;
                
                public class PhoneInfo {
                    public String getHelloJava() {
                            return "Hello java";
                        }
                
                    public String getPhoneNumber(Context context){
                            TelephonyManager tMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                            String mPhoneNumber = tMgr.getLine1Number();
                            return mPhoneNumber;
                        }
                }
                

                And, on C++ side:

                if(QAndroidJniObject::isClassAvailable("JavaApi/PhoneInfo"))
                    {
                        QAndroidJniObject someJavaObject = QAndroidJniObject("JavaApi/PhoneInfo");
                        qDebug() << someJavaObject.callObjectMethod<jstring>("getHelloJava").toString();
                        qDebug() << someJavaObject.callObjectMethod<jstring>(
                                                        "getPhoneNumber", 
                                                        "(Landroid/content/Context;)Ljava/lang/String;",
                                                        QtAndroid::androidContext()
                                                         ).toString();
                    }
                
                C Offline
                C Offline
                Creatorczyk
                wrote on 12 Apr 2022, 13:45 last edited by
                #7

                @KroMignon I got error like bellow when I tried your code:

                /media/sf_GitLab/app/app/main.cpp:41: error: no matching member function for call to 'callObjectMethod'
                ../app/main.cpp:41:36: error: no matching member function for call to 'callObjectMethod'
                        qDebug() << someJavaObject.callObjectMethod<jstring>(
                                    ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
                /home/john/Qt/5.15.2/android/include/QtAndroidExtras/qandroidjniobject.h:74:23: note: candidate function template not viable: requires single argument 'methodName', but 3 arguments were provided
                    QAndroidJniObject callObjectMethod(const char *methodName) const;
                
                                  ^
                
                K 1 Reply Last reply 12 Apr 2022, 13:55
                0
                • C Creatorczyk
                  12 Apr 2022, 13:45

                  @KroMignon I got error like bellow when I tried your code:

                  /media/sf_GitLab/app/app/main.cpp:41: error: no matching member function for call to 'callObjectMethod'
                  ../app/main.cpp:41:36: error: no matching member function for call to 'callObjectMethod'
                          qDebug() << someJavaObject.callObjectMethod<jstring>(
                                      ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
                  /home/john/Qt/5.15.2/android/include/QtAndroidExtras/qandroidjniobject.h:74:23: note: candidate function template not viable: requires single argument 'methodName', but 3 arguments were provided
                      QAndroidJniObject callObjectMethod(const char *methodName) const;
                  
                                    ^
                  
                  K Offline
                  K Offline
                  KroMignon
                  wrote on 12 Apr 2022, 13:55 last edited by KroMignon 4 Dec 2022, 13:55
                  #8

                  @Creatorczyk said in Qt Android get phone number:

                  I got error like bellow when I tried your code:

                  Sorry my mistake, should be:

                  if(QAndroidJniObject::isClassAvailable("JavaApi/PhoneInfo"))
                      {
                          QAndroidJniObject javaString;
                          QAndroidJniObject someJavaObject = QAndroidJniObject("JavaApi/PhoneInfo");
                  
                          javaString = someJavaObject.callObjectMethod("getHelloJava", "()Ljava/lang/String;");
                          qDebug() << "getHelloJava() returns: " << (javaString .isValid() ? javaString.toString() : "***ERROR***");
                  
                          javaString = someJavaObject.callObjectMethod(
                                                          "getPhoneNumber", 
                                                          "(Landroid/content/Context;)Ljava/lang/String;",
                                                          QtAndroid::androidContext()
                                                           );
                          qDebug() << "getPhoneNumber() returns:" << (javaString .isValid() ? javaString.toString() : "***ERROR***"); 
                      }
                  

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

                  C 1 Reply Last reply 13 Apr 2022, 07:02
                  0
                  • K KroMignon
                    12 Apr 2022, 13:55

                    @Creatorczyk said in Qt Android get phone number:

                    I got error like bellow when I tried your code:

                    Sorry my mistake, should be:

                    if(QAndroidJniObject::isClassAvailable("JavaApi/PhoneInfo"))
                        {
                            QAndroidJniObject javaString;
                            QAndroidJniObject someJavaObject = QAndroidJniObject("JavaApi/PhoneInfo");
                    
                            javaString = someJavaObject.callObjectMethod("getHelloJava", "()Ljava/lang/String;");
                            qDebug() << "getHelloJava() returns: " << (javaString .isValid() ? javaString.toString() : "***ERROR***");
                    
                            javaString = someJavaObject.callObjectMethod(
                                                            "getPhoneNumber", 
                                                            "(Landroid/content/Context;)Ljava/lang/String;",
                                                            QtAndroid::androidContext()
                                                             );
                            qDebug() << "getPhoneNumber() returns:" << (javaString .isValid() ? javaString.toString() : "***ERROR***"); 
                        }
                    
                    C Offline
                    C Offline
                    Creatorczyk
                    wrote on 13 Apr 2022, 07:02 last edited by
                    #9

                    @KroMignon I received error like below:
                    2aecd04c-1b79-4ba4-80cb-dde5d0b1ee4f-image.png

                    K 1 Reply Last reply 13 Apr 2022, 07:04
                    0
                    • C Creatorczyk
                      13 Apr 2022, 07:02

                      @KroMignon I received error like below:
                      2aecd04c-1b79-4ba4-80cb-dde5d0b1ee4f-image.png

                      K Offline
                      K Offline
                      KroMignon
                      wrote on 13 Apr 2022, 07:04 last edited by KroMignon
                      #10

                      @Creatorczyk Hmm maybe I should take more time before posting!

                      Should be:

                      javaString = someJavaObject.callObjectMethod(
                                                              "getPhoneNumber", 
                                                              "(Landroid/content/Context;)Ljava/lang/String;",
                                                              QtAndroid::androidContext().object<jobject>()
                                                               );
                      

                      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
                      0

                      1/10

                      12 Apr 2022, 12:17

                      • Login

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