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. How to send SMS in android?

How to send SMS in android?

Scheduled Pinned Locked Moved Mobile and Embedded
36 Posts 12 Posters 5.6k 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on 22 May 2019, 13:36 last edited by
    #12

    How to call a method in Qt from java SmsManager::createAppSpecificSmsTokenWithPackageinfo(String prefixes, PendingIntent intent)

    J 1 Reply Last reply 22 May 2019, 13:56
    0
    • M Mikeeeeee
      22 May 2019, 13:36

      How to call a method in Qt from java SmsManager::createAppSpecificSmsTokenWithPackageinfo(String prefixes, PendingIntent intent)

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 22 May 2019, 13:56 last edited by
      #13

      @Mikeeeeee Take a look at https://doc.qt.io/qt-5/qandroidjniobject.html

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

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mikeeeeee
        wrote on 22 May 2019, 15:48 last edited by
        #14

        Will this option work, or should we do it differently?

            QAndroidJniObject testSms;
            testSms.callMethod< jint > ("createAppSpecificSmsTokenWithPackageInfo(32133)");
        
        P J 2 Replies Last reply 22 May 2019, 16:22
        0
        • M Mikeeeeee
          22 May 2019, 15:48

          Will this option work, or should we do it differently?

              QAndroidJniObject testSms;
              testSms.callMethod< jint > ("createAppSpecificSmsTokenWithPackageInfo(32133)");
          
          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 22 May 2019, 16:22 last edited by
          #15

          @Mikeeeeee said in How to send SMS in android?:

          Will this option work

          Have you already tried? Trial and error is a way to learn...

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2
          • M Offline
            M Offline
            Mikeeeeee
            wrote on 22 May 2019, 17:21 last edited by
            #16

            It doesn't work. How can this be fixed to work?

            1 Reply Last reply
            0
            • M Mikeeeeee
              22 May 2019, 15:48

              Will this option work, or should we do it differently?

                  QAndroidJniObject testSms;
                  testSms.callMethod< jint > ("createAppSpecificSmsTokenWithPackageInfo(32133)");
              
              J Offline
              J Offline
              JonB
              wrote on 22 May 2019, 18:19 last edited by JonB
              #17

              @Mikeeeeee

                  QAndroidJniObject testSms;
                  testSms.callMethod< jint > ("createAppSpecificSmsTokenWithPackageInfo(32133)");
              

              With the warning that I know absolutely nothing about this:

              You are using the overload https://doc.qt.io/qt-5/qandroidjniobject.html#callMethod, callMethod(const char *methodName). It may well be that methodName has to be a method name, "createAppSpecificSmsTokenWithPackageInfo(32133)" is a full function call. You may have to use overload https://doc.qt.io/qt-5/qandroidjniobject.html#callMethod-1, callMethod(const char *methodName, const char *sig, ...), and specify the function signature and pass 32133 as parameter, in order to call functions with parameters?

              Have you/can you test if you can call a method which does not take any parameters, does that work OK?

              1 Reply Last reply
              2
              • M Offline
                M Offline
                Mikeeeeee
                wrote on 23 May 2019, 11:30 last edited by
                #18

                The SmsManager class has a getcarrierconfigvalues() function without an argument.
                This code also generates an error:

                    QAndroidJniObject testSms;
                    testSms.callMethod< jint > ("getCarrierConfigValues()");
                
                J J 2 Replies Last reply 23 May 2019, 12:00
                0
                • M Mikeeeeee
                  23 May 2019, 11:30

                  The SmsManager class has a getcarrierconfigvalues() function without an argument.
                  This code also generates an error:

                      QAndroidJniObject testSms;
                      testSms.callMethod< jint > ("getCarrierConfigValues()");
                  
                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 23 May 2019, 12:00 last edited by
                  #19

                  @Mikeeeeee said in How to send SMS in android?:

                  generates an error

                  Please post the error

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

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    Mikeeeeee
                    wrote on 23 May 2019, 12:12 last edited by
                    #20

                    "org.qt project.example.smsTest" crashed.

                    J K 2 Replies Last reply 23 May 2019, 12:21
                    0
                    • M Mikeeeeee
                      23 May 2019, 12:12

                      "org.qt project.example.smsTest" crashed.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 23 May 2019, 12:21 last edited by
                      #21

                      @Mikeeeeee Does it crash when

                      testSms.callMethod< jint > ("getCarrierConfigValues()");
                      

                      is executed?
                      Also consider that some APIs require your app to request access rights from the user.

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

                      1 Reply Last reply
                      1
                      • M Mikeeeeee
                        23 May 2019, 12:12

                        "org.qt project.example.smsTest" crashed.

                        K Offline
                        K Offline
                        KroMignon
                        wrote on 23 May 2019, 12:24 last edited by
                        #22

                        @Mikeeeeee If you remove all the "SMS sending" stuff from your source code, does it compile and can you start it on your device (or emulator) ?

                        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
                        • K Offline
                          K Offline
                          KroMignon
                          wrote on 23 May 2019, 12:36 last edited by KroMignon
                          #23

                          @Mikeeeeee When you have a working Qt Android App, adding SMS sending support is not that compilcated.

                          1. ensure Android Extras are enabled (in pro file, add android: QT += androidextras)
                          2. ensure you have add required permission in AndroidManifest.xml (android.permission.SEND_SMS )
                          3. Add following code to the class you want to use to send SMS, for example JniHandler:
                          #include <QtAndroid>
                          #include <QAndroidJniObject>
                          #include <QtAndroidExtras/QAndroidJniObject>
                          #include <QtAndroidExtras/QAndroidJniEnvironment>
                          #include <jni.h>
                          ...
                          
                          void JniHandler::sendSMS(const QString& phoneNumber, const QString& message)
                          {
                              QtAndroid::runOnAndroidThreadSync([phoneNumber, message]  {
                                  // get the Qt android activity
                                  QAndroidJniObject activity = QtAndroid::androidActivity();
                                  if (activity.isValid()){
                                      // get the default SmsManager
                                      QAndroidJniObject mySmsManager = QAndroidJniObject::callStaticObjectMethod("android/telephony/SmsManager",
                                                                                                                 "getDefault",
                                                                                                                 "()Landroid/telephony/SmsManager;" );
                                      // get phone number & text from UI and convert to Java String
                                      QAndroidJniObject myPhoneNumber = QAndroidJniObject::fromString(phoneNumber);
                                      QAndroidJniObject myTextMessage = QAndroidJniObject::fromString(message);
                                      QAndroidJniObject scAddress = NULL;
                                      QAndroidJniObject sentIntent = NULL;
                                      QAndroidJniObject deliveryIntent = NULL;
                                  
                                      // call the java function:
                                      // public void SmsManager.sendTextMessage(String destinationAddress,
                                      //                                        String scAddress, String text,
                                      //                                        PendingIntent sentIntent, PendingIntent deliveryIntent)
                                      // see: http://developer.android.com/reference/android/telephony/SmsManager.html
                                  
                                      mySmsManager.callMethod<void>("sendTextMessage",
                                                                    "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/app/PendingIntent;Landroid/app/PendingIntent;)V",
                                                                     myPhoneNumber.object<jstring>(),
                                                                     scAddress.object<jstring>(),
                                                                     myTextMessage.object<jstring>(), NULL, NULL );
                                  
                                        }
                                  else {
                                      qDebug() << "Something wrong with Qt activity...";
                                  }
                              }
                          }
                          

                          This should do the job.

                          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
                          4
                          • M Offline
                            M Offline
                            Mikeeeeee
                            wrote on 23 May 2019, 14:43 last edited by
                            #24

                            @KroMignon said in How to send SMS in android?:

                            android.permission.SEND_SMS

                            android.permission.SEND_SMS must be added to .pro file?

                            K 1 Reply Last reply 23 May 2019, 15:01
                            0
                            • M Mikeeeeee
                              23 May 2019, 14:43

                              @KroMignon said in How to send SMS in android?:

                              android.permission.SEND_SMS

                              android.permission.SEND_SMS must be added to .pro file?

                              K Offline
                              K Offline
                              KroMignon
                              wrote on 23 May 2019, 15:01 last edited by
                              #25

                              @Mikeeeeee said in How to send SMS in android?:

                              android.permission.SEND_SMS must be added to .pro file?

                              No, it must be added into the manifest (AndroidManifest.xml) ==> editing-manifest-files

                              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
                              2
                              • M Offline
                                M Offline
                                Mikeeeeee
                                wrote on 23 May 2019, 15:50 last edited by
                                #26

                                Void MainWindow::sendSMS is missing ")". Added so and still not working. What's right?

                                void MainWindow::sendSMS(const QString& phoneNumber, const QString& message)
                                {
                                    QtAndroid::runOnAndroidThreadSync([phoneNumber, message]  {
                                        // get the Qt android activity
                                        QAndroidJniObject activity = QtAndroid::androidActivity();
                                        if (activity.isValid()){
                                            // get the default SmsManager
                                            QAndroidJniObject mySmsManager = QAndroidJniObject::callStaticObjectMethod("android/telephony/SmsManager",
                                                                                                                       "getDefault",
                                                                                                                       "()Landroid/telephony/SmsManager;" );
                                            // get phone number & text from UI and convert to Java String
                                            QAndroidJniObject myPhoneNumber = QAndroidJniObject::fromString(phoneNumber);
                                            QAndroidJniObject myTextMessage = QAndroidJniObject::fromString(message);
                                            QAndroidJniObject scAddress = NULL;
                                            QAndroidJniObject sentIntent = NULL;
                                            QAndroidJniObject deliveryIntent = NULL;
                                
                                            // call the java function:
                                            // public void SmsManager.sendTextMessage(String destinationAddress,
                                            //                                        String scAddress, String text,
                                            //                                        PendingIntent sentIntent, PendingIntent deliveryIntent)
                                            // see: http://developer.android.com/reference/android/telephony/SmsManager.html
                                
                                            mySmsManager.callMethod<void>("sendTextMessage",
                                                                          "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/app/PendingIntent;Landroid/app/PendingIntent;)V",
                                                                           myPhoneNumber.object<jstring>(),
                                                                           scAddress.object<jstring>(),
                                                                           myTextMessage.object<jstring>(), NULL, NULL );
                                
                                              }
                                        else {
                                            qDebug() << "Something wrong with Qt activity...";
                                        }
                                    });
                                }
                                
                                K 1 Reply Last reply 23 May 2019, 15:54
                                0
                                • M Mikeeeeee
                                  23 May 2019, 15:50

                                  Void MainWindow::sendSMS is missing ")". Added so and still not working. What's right?

                                  void MainWindow::sendSMS(const QString& phoneNumber, const QString& message)
                                  {
                                      QtAndroid::runOnAndroidThreadSync([phoneNumber, message]  {
                                          // get the Qt android activity
                                          QAndroidJniObject activity = QtAndroid::androidActivity();
                                          if (activity.isValid()){
                                              // get the default SmsManager
                                              QAndroidJniObject mySmsManager = QAndroidJniObject::callStaticObjectMethod("android/telephony/SmsManager",
                                                                                                                         "getDefault",
                                                                                                                         "()Landroid/telephony/SmsManager;" );
                                              // get phone number & text from UI and convert to Java String
                                              QAndroidJniObject myPhoneNumber = QAndroidJniObject::fromString(phoneNumber);
                                              QAndroidJniObject myTextMessage = QAndroidJniObject::fromString(message);
                                              QAndroidJniObject scAddress = NULL;
                                              QAndroidJniObject sentIntent = NULL;
                                              QAndroidJniObject deliveryIntent = NULL;
                                  
                                              // call the java function:
                                              // public void SmsManager.sendTextMessage(String destinationAddress,
                                              //                                        String scAddress, String text,
                                              //                                        PendingIntent sentIntent, PendingIntent deliveryIntent)
                                              // see: http://developer.android.com/reference/android/telephony/SmsManager.html
                                  
                                              mySmsManager.callMethod<void>("sendTextMessage",
                                                                            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/app/PendingIntent;Landroid/app/PendingIntent;)V",
                                                                             myPhoneNumber.object<jstring>(),
                                                                             scAddress.object<jstring>(),
                                                                             myTextMessage.object<jstring>(), NULL, NULL );
                                  
                                                }
                                          else {
                                              qDebug() << "Something wrong with Qt activity...";
                                          }
                                      });
                                  }
                                  
                                  K Offline
                                  K Offline
                                  KroMignon
                                  wrote on 23 May 2019, 15:54 last edited by KroMignon
                                  #27

                                  @Mikeeeeee What do you mean with "not working"? Do you have an error message? Is there an application crash?

                                  Just say "not working" does not give any information to help you!

                                  PS: Sorry for the missed parenthese

                                  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
                                    Mikeeeeee
                                    wrote on 23 May 2019, 16:56 last edited by
                                    #28

                                    Error: "org.qtproject.example" crashed.

                                    K 1 Reply Last reply 24 May 2019, 05:53
                                    0
                                    • M Mikeeeeee
                                      23 May 2019, 16:56

                                      Error: "org.qtproject.example" crashed.

                                      K Offline
                                      K Offline
                                      KroMignon
                                      wrote on 24 May 2019, 05:53 last edited by KroMignon
                                      #29

                                      @Mikeeeeee said in How to send SMS in android?:

                                      Error: "org.qtproject.example" crashed.

                                      That's also not helpful, we can not see what you are doing, if you want help, than you must explain what is the problem and what is your development environment/settings:

                                      • which Qt Kit are you using to build your project? (for example Android for armeabi-v7a (Clang Qt 5.7.1 for Android armv7)
                                      • which Qt Creator Version you are using
                                      • what is your target device? It is a smartphone, a tablet or an emulator?
                                      • what Android version are you targeting?
                                      • when does the application crash? When you starting the application or when you try to send a SMS?

                                      You give nothing to enable us to help you, this is all up to you.

                                      Last, but not least, is your device capable to send SMS?

                                      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
                                      2
                                      • M Offline
                                        M Offline
                                        Mikeeeeee
                                        wrote on 24 May 2019, 08:21 last edited by
                                        #30
                                        1. I use Android for armeabi-v7a (Clang Qt 5.12.3 for Android armv7)
                                        2. I use Qt Creator 5.12.2
                                        3. Target device - smartphone
                                        4. Android version 6
                                        5. Crash application when I start the function of sending SMS
                                        6. My smartphone is able to send SMS
                                        1 Reply Last reply
                                        0
                                        • M Mikeeeeee
                                          23 May 2019, 11:30

                                          The SmsManager class has a getcarrierconfigvalues() function without an argument.
                                          This code also generates an error:

                                              QAndroidJniObject testSms;
                                              testSms.callMethod< jint > ("getCarrierConfigValues()");
                                          
                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 27 May 2019, 10:57 last edited by JonB
                                          #31

                                          @Mikeeeeee said in How to send SMS in android?:

                                          The SmsManager class has a getcarrierconfigvalues() function without an argument.
                                          This code also generates an error:

                                              QAndroidJniObject testSms;
                                              testSms.callMethod< jint > ("getCarrierConfigValues()");
                                          

                                          Have only just seen this. As I said, and as the documentation states, the argument is supposed (according to my understanding) to be a method name, so getCarrierConfigValues and not getCarrierConfigValues(), or any such with parentheses. I don't know where you are in your investigations, but did you at least try something like that once? If you have gotten further through since then, you may ignore.

                                          1 Reply Last reply
                                          2

                                          21/36

                                          23 May 2019, 12:21

                                          • Login

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