|
|
For TotalCross 3.42, we created a way to manage SMS in Android. The example is on [GitHub](https://github.com/TotalCross/SmsManagerSample). 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()`](https://github.com/TotalCross/SmsManagerSample/blob/master/src/main/java/totalcross/sample/smsmanager/app/SmsManagerSample.java#L71).
|
|
|
|
|
|
## 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 it `null`;
|
|
|
- `text` is the message that will de sent to the number `destinationAddress` using the service center `scAddress`.
|
|
|
|
|
|
You can check in the example how [it is used](https://github.com/TotalCross/SmsManagerSample/blob/master/src/main/java/totalcross/sample/smsmanager/app/SmsManagerSample.java#L54)
|
|
|
|
|
|
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](https://github.com/TotalCross/SmsManagerSample/blob/master/src/main/java/totalcross/sample/smsmanager/app/SmsManagerSample.java#L73). More details of the `SmsMessage` [further on](#smsmessage).
|
|
|
|
|
|
With your receiver in hand, you must tell the `SmsManager` who it is with the `registerSmsReceiver(SmsReceiver)` method, like [in the example](https://github.com/TotalCross/SmsManagerSample/blob/master/src/main/java/totalcross/sample/smsmanager/app/SmsManagerSample.java#L72).
|
|
|
|
|
|
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:
|
|
|
|
|
|
```java
|
|
|
// repaint on main window
|
|
|
MainWindow.getMainWindow().runOnMainThread(() -> {
|
|
|
topContainer.repaintNow();
|
|
|
});
|
|
|
```
|
|
|
|
|
|
You can see it in the examples, we change the UI between lines [74-79](https://github.com/TotalCross/SmsManagerSample/blob/master/src/main/java/totalcross/sample/smsmanager/app/SmsManagerSample.java#L74-L79) and we ask to repaint the control between lines [81-85](https://github.com/TotalCross/SmsManagerSample/blob/master/src/main/java/totalcross/sample/smsmanager/app/SmsManagerSample.java#L81..L85)
|
|
|
|
|
|
Please note that you can only have a single class intercepting SMS at a time; calling the method `SmsManager.registerSmsReceiver(SmsReceiver)` will replace the older `SmsReceiver` with the new one. If you wish to stop intercepting SMS, pass `null` as the argument to `registerSmsReceiver`.
|
|
|
|
|
|
### `SmsMessage` |
|
|
\ No newline at end of file |