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 Updated to NodeBB v4.3 + New Features

Qt Android get phone number

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
10 Posts 4 Posters 984 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 last edited by Creatorczyk
    #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.

    jsulmJ J.HilkJ KroMignonK 3 Replies Last reply
    0
    • C Creatorczyk

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      0
      • jsulmJ jsulm

        @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 last edited by
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • C Creatorczyk

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

            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.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on 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

              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.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #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
              1
              • KroMignonK KroMignon

                @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 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;
                
                                  ^
                
                KroMignonK 1 Reply Last reply
                0
                • C Creatorczyk

                  @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;
                  
                                    ^
                  
                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by KroMignon
                  #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
                  0
                  • KroMignonK KroMignon

                    @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 last edited by
                    #9

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

                    KroMignonK 1 Reply Last reply
                    0
                    • C Creatorczyk

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

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on 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

                      • Login

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