How to send SMS in android?
-
Might be a silly question but are you building that with your Qt Android kit ?
-
That has nothing to do with your error. You were using a Qt build that was not for Android, likely your desktop build.
MinGW is unrelated to the Qt Android version. The compiler is provided by the Android NDK.
-
How to call a method in Qt from java SmsManager::createAppSpecificSmsTokenWithPackageinfo(String prefixes, PendingIntent intent)
@Mikeeeeee Take a look at https://doc.qt.io/qt-5/qandroidjniobject.html
-
Will this option work, or should we do it differently?
QAndroidJniObject testSms; testSms.callMethod< jint > ("createAppSpecificSmsTokenWithPackageInfo(32133)");
@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...
-
Will this option work, or should we do it differently?
QAndroidJniObject testSms; testSms.callMethod< jint > ("createAppSpecificSmsTokenWithPackageInfo(32133)");
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 thatmethodName
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 pass32133
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?
-
The SmsManager class has a getcarrierconfigvalues() function without an argument.
This code also generates an error:QAndroidJniObject testSms; testSms.callMethod< jint > ("getCarrierConfigValues()");
-
@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. -
@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) ?
-
@Mikeeeeee When you have a working Qt Android App, adding SMS sending support is not that compilcated.
- ensure Android Extras are enabled (in pro file, add
android: QT += androidextras
) - ensure you have add required permission in AndroidManifest.xml (
android.permission.SEND_SMS
) - 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.
- ensure Android Extras are enabled (in pro file, add
-
@KroMignon said in How to send SMS in android?:
android.permission.SEND_SMS
android.permission.SEND_SMS must be added to .pro file?
-
@KroMignon said in How to send SMS in android?:
android.permission.SEND_SMS
android.permission.SEND_SMS must be added to .pro file?
@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 -
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..."; } }); }
-
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..."; } }); }
@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