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 read / send sms for QT Widget application & C++

How to read / send sms for QT Widget application & C++

Scheduled Pinned Locked Moved Mobile and Embedded
smsandroidc++qt widgettelephony class
6 Posts 3 Posters 11.4k 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.
  • Q Offline
    Q Offline
    QTUserForAndroid
    wrote on 19 Jan 2016, 20:12 last edited by QTUserForAndroid 4 Dec 2016, 20:27
    #1

    Hi, I am asking help on reading and sending sms form qt application written in C++ for Android device. I am quite new to QT, and need some pointers in the right direction how to do this.

    I found a few things like
    Telephony class or
    Easy Content providers https://github.com/EverythingMe/easy-content-providers#sample-app (looks like a JAVA sample program)

    Please help me with pointers what is the easiest way to do this, and if possible can you post a example. (Please include the #includes, header file and other changes needed).

    Any help would be appreciated, to get me going.

    Many thanks

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 20 Jan 2016, 07:54 last edited by
      #2

      you will need to call the Android API directly from Qt, using QtAndroidExtras module. Search for "Qt JNI" and additionally how to read SMS on android on the web for more resources.
      Basically you need to create a JAVA file in your app with a method which does what you want and simply call this method via JNI from your Qt code.

      --- 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

      Q 1 Reply Last reply 1 Mar 2016, 19:48
      0
      • R raven-worx
        20 Jan 2016, 07:54

        you will need to call the Android API directly from Qt, using QtAndroidExtras module. Search for "Qt JNI" and additionally how to read SMS on android on the web for more resources.
        Basically you need to create a JAVA file in your app with a method which does what you want and simply call this method via JNI from your Qt code.

        Q Offline
        Q Offline
        QTUserForAndroid
        wrote on 1 Mar 2016, 19:48 last edited by
        #3

        Hi, i tried the JNI example, and got it working, but JNI is only for static JAVA calls. All the sms send /receive examples i could find (java) use a non static call. So i investigated how to do a non static call, but no success so far.

        After many many searches on the web, i found a article (in French) how to send a sms with SMSManager Class, calling the API function SendTextMessage() directly from QT C++ code. This works perfectly, but unfortunately only give a example on a send sms.
        http://grimaldi.univ-tln.fr/une-application-qt-android-qui-envoie-des-sms.html

        So my Question: Can the smsmanager class receive sms? I do not see a function call for this
        http://developer.android.com/reference/android/telephony/SmsManager.html

        Or do i need to use something like smsmessage class
        http://developer.android.com/reference/android/telephony/SmsMessage.html

        Except for the send sms example (French site), i do not find any examples on the web how to send and receive sms via a QT Widget C++ application for Android? Why is no one using it? I must be looking at the wrong places!! I do find several java examples (non static) but cannot get the call to the java non static function from QT working.

        I would like to

        1. Read my Android phone inbox
        2. Retrieve sms messages as they arrive.

        Any help will be appreciated, a QT example would be much appreciated. I am really stuck now after weeks of trying and searching the web.

        Thanks

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          QTUserForAndroid
          wrote on 12 Apr 2016, 20:22 last edited by QTUserForAndroid 4 Dec 2016, 20:59
          #4

          OK, no one is replying to this problem, nor can i find any example on the web. But what i did manage to get working is to call a static JAVA procedure from QT (JNI), and the Java Procedure then calls a non static JAVA procedure to send a sms, or receive a sms.
          The send sms is working 100%, but i cannot get the receive sms to work. I guess the receive sms JAVA code must be a class of its own? It is compiling without a error but does not work. Crash in the java procedure sms_receive(int n)

          Is there anyone who can help me. U need to be a JAVA expert to get this going!!

          Thanks, here is my code so far.

          Mainwindow.cpp file
          
          #include <QtAndroidExtras/QAndroidJniObject> // For JNI, need to select Android Build
          #include <QAndroidJniObject>
          
          int My_Procedure_Call_SendSMS(int n)
          {
          int Answer1;
          Answer1 =  QAndroidJniObject::callStaticMethod<jint>("org/qtproject/qt5/android/bindings/MyActivity","JAVA_Send_SMS","(I)I",n);
          return Answer1;
          }
          
          int My_Procedure_Call_ReceiveSMS(int n)
          {
          int Answer2;
          Answer2 =  QAndroidJniObject::callStaticMethod<jint>("org/qtproject/qt5/android/bindings/MyActivity","JAVA_Receive_SMS","(I)I",n);
          return Answer2;
          }
          
          MyActivity.java
          
          import org.qtproject.qt5.android.bindings.QtActivity;
          import android.content.Intent;
          import android.os.Bundle;
          import android.util.Log;
          import java.lang.String;
          
          import android.app.Activity;
          
          
          public class MyActivity extends QtActivity // This is the class
          {
          
          // Instantiation of this class
          private static MyActivity m_instance;
          public MyActivity()
          {
              m_instance = this;
          }
          
          public static int JAVA_Send_SMS(int n)
          {
          int Answer1;
          
              Log.d(QtApplication.QtTAG, "JAVA_Send_SMS() entered");
              My_Java_Procedures java_procedures = new My_Java_Procedures();
              Answer1 = java_procedures.sms_send(n);
              return Answer1;
          }
          public static int JAVA_Receive_SMS(int n)
          {
          int Answer2;
          
              Log.d(QtApplication.QtTAG, "JAVA_Receive_SMS() entered");
              My_Java_Procedures java_procedures = new My_Java_Procedures();
              Answer2 = java_procedures.sms_receive(n);
              return Answer2;
          }
          }
          My_Java_Procedures.java
          package org.qtproject.qt5.android.bindings;
          
          import android.app.Activity;
          import android.content.ContentResolver;
          import android.database.Cursor;
          import android.net.Uri;
          import android.os.Bundle;
          import android.view.View;
          import android.view.View.OnClickListener;
          import android.widget.Button;
          import android.widget.ListView;
          import android.widget.SimpleCursorAdapter;
          import android.widget.TextView;
          
          import android.content.Intent;
          
          import java.util.HashSet;
          import java.util.Set;
          import java.util.StringTokenizer;
          
          import android.content.BroadcastReceiver;
          import android.content.Context;
          import android.content.Intent;
          
          import android.os.Bundle;
          import android.preference.PreferenceManager;
          import android.telephony.SmsMessage;
          
          import android.telephony.SmsManager; // For sending sms
          
          public class My_Java_Procedures {
          
          // Call Instantiation
          // **********************************
          public My_Java_Procedures My_Java_Procedures_instance;
          {
              My_Java_Procedures_instance = this;
          }
          // **********************************
          
          public int sms_send(int n)
          {
          // Remember to add your permisions to Android Manifest file
          System.out.println("Eureka Send");
          
          // http://pulse7.net/android/send-sms-message-android/
          // Use import android.telephony.SmsManager;
          
          // Initialize SmsManager Object
          SmsManager smsManager = SmsManager.getDefault();
          
          // Send Message using method of SmsManager object
          smsManager.sendTextMessage("your cell number", null,"Message Body", null, null);
          return n+10;
          }
          
          
          //public int sms_receive(Context context,Intent intent,int n)
          public int sms_receive(int n)
          {
              System.out.println("Debug message Receive SMS");
          
              // http://stackoverflow.com/questions/19856338/using-new-telephony-content-provider-to-read-sms
              Activity act = new Activity();
              //Intent intent = new Intent(this, DisplayMessageActivity.class);
              Cursor cursor = act.getApplicationContext().getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
          
              System.out.println("Debug 2");
              //ContentResolver resolver = getContentResolver();
              //Cursor cursor = resolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
          
          // http://www.learn-android-easily.com/2013/04/accessing-inbox-in-android.html
          //    String senderNumber=cursor.getString(cursor.getColumnIndex("address"));
          //    String smsBody=cursor.getString(cursor.getColumnIndex("body"));
          //    String dateTime=cursor.getString(cursor.getColumnIndex("date"));
          
              if (cursor.moveToFirst()) { // must check the result to prevent exception
              System.out.println("Debug 3");
                  do {
                     String msgData = "";
                     for(int idx=0;idx<cursor.getColumnCount();idx++)
                     {
                         msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
                         System.out.println(msgData);
                     }
                     // use msgData
                  } while (cursor.moveToNext());
              } else {
                 // empty box, no SMS
              }
          
          return n+100;
          }
          } // End Class
          
          
          1 Reply Last reply
          0
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on 13 Apr 2016, 05:56 last edited by kuzulis
            #5

            Send SMS:

            void Widget::on_pushButton_clicked()
            {
                // get the Qt android activity
                QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative",
                                                                                        "activity",
                                                                                        "()Landroid/app/Activity;");
                if (activity.isValid()){
            
                    //get the default SmsManager
                    QAndroidJniObject mySmsManager = QAndroidJniObject::callStaticObjectMethod("android/telephony/SmsManager",
                                                                                               "getDefault",
                                                                                               "()Landroid/telephony/SmsManager;");
                    if (!mySmsManager.isValid()) {
                        qDebug() << "Something wrong with SMS manager...";
                    } else {
            
                        // get phone number & text from UI and convert to Java String
                        QAndroidJniObject myPhoneNumber = QAndroidJniObject::fromString(ui->lineEditDestinataire->text());
                        QAndroidJniObject myTextMessage = QAndroidJniObject::fromString(ui->lineEditTexte->text());
                        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...";
                }
            } 
            
            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              QTUserForAndroid
              wrote on 14 Apr 2016, 16:12 last edited by QTUserForAndroid
              #6

              Thanks

              I tried your example on how to send sms. Unfortunately the application crash when mySmsManager.callMethod<void>("sendTextMessage",.... is executed. Builds correctly.

              But i think i have a problem with Qt 5.6 with Qt Extras not installed. Will go back to Qt 5.1.1 and test. Did test in Qt 5.5.1 Same crash problem. Probably some installation or package i need?

              The only includes i added was
              #include <QDebug>
              #include <QtAndroidExtras/QAndroidJniObject> // For JNI, need to select Android Build
              #include <QAndroidJniObject>

              and added the QT extras in the Pro file

              You don't perhaps have some clue or example on how to read the Android SMS inbox, and how to receive incomming sms on the fly?

              Thanks

              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