New to TotalCross? Check our homepage and learn more about us!
- For TotalCross 3.42, we created a way to manage SMS in Android.
- For Totalcross 4.1.0, we implements sending and receiving data SMS also disables changing the state of the receiver when the application is paused and resumed
You can see the example on GitHub. iOS does not allow handling SMS.
The SmsManager
class allows you to send a SMS message and to intercept a received SMS. The SmsManager
is a singleton that could be different depending on the current platform. You should get its preferred instance with SmsManager.getDefault()
.
Send a SMS
To send a SMS, you must call the sendTextMessage(String destinationAddress, String scAddress, String text)
method from the SmsManager
object. Let us dive in this subject here:
-
destinationAddress
is the phone number targeted to receive the message; -
scAddress
is the service center address; if you want to use the default service, just keep itnull
; -
text
is the message that will de sent to the numberdestinationAddress
using the service centerscAddress
.
You can check in the example how it is used
When you call this method, your application will call the system's default SMS app.
Intercept received SMS
To intercept a received SMS, you should tell your SmsManager
who will do this work for you. The one who will do it is an object that implements the SmsReceiver
interface. It is a simple interface with only one method onReceive(SmsMessage)
, so in the example we created it using lambda. More details of the SmsMessage
further on.
With your receiver in hand, you must tell the SmsManager
who it is with the registerSmsReceiver(SmsReceiver)
or registerSmsReceiver(SmsReceiver receiver, int port)
method, like in the example.
If you wish to change something live in the UI, there are some caveats. You should do all the manipulation of the UI as you wish, and only after it is done asks the controls to repaint themselves, but this repaint must run in the main thread:
// repaint on main window
MainWindow.getMainWindow().runOnMainThread(() -> {
topContainer.repaintNow();
});
Please note that you can only have a single class intercepting SMS at a time; calling the method SmsManager.registerSmsReceiver(SmsReceiver)
or SmsManager.registerSmsReceiver(SmsReceiver receiver, int port)
- will replace the older
SmsReceiver
with the new one. If you wish to stop intercepting SMS, passnull
as the argument toregisterSmsReceiver
. - the
port
is required to listen to incoming SMS messages and SMS data messages. If you want to listen on the default port, just need a pass -1port
for text messages.
You can see it in the examples, we change the UI between lines 74-79 and we ask to repaint the control between lines 81-85
SmsMessage
object
To handle an intercepted SMS, you may want to read it's content and do something with it. It is an object created by the TotalCross VM that contains some info about the SMS. That info are the message body and the originating address. These are available in their respective getter
s:
-
getDisplayOriginatingAddress()
\rightarrow
originating address -
getDisplayMessageBody()
\rightarrow
message body
We use both of getter
s in the Github example.