Hacking Titanium Mobile for fun and profit

Using Intents for Sending Sms Messages on Android With Ti Mobile

Titanium mobile historically lacks of support for in-app sms sending facilities. Even the availability of the MFMessageComposeViewController class since the release of iOS 4.X didn’t push Appcelerator guys to provide an appropriate mapping in the framework API. I personally tried to overcome the issue (at least on iOS) by creating a custom native module, which had a quite good response from the community.

After releasing the iOS module I started investigating how I could provide a similar feature also for Android devices. It turns out that programmatically sending text messages on Android can be performed in a couple of ways:

Talking about native code, the simplest way for doing it is the latter, and all can be done with at most 4 lines of code. Since the Titanium SDK provides a direct mapping on native Android intents from release 1.5.0, implementing the same technique in a Ti Mobile project is quite straightforward:

What this snippet of code does is simply to create a Ti.Android.Intent object with the appropriate properties and start an activity for it. As a result, the SMS sending activity registered in the system (i.e. probably that of the default SMS messaging app) gets started, pre-populated with the provided recipient (address) and message body (sms_body). That’s it.

This solution is very simple and powerful, with the only drawback that there’s no possibility to get notified about the result of the operation, so we won’t know if the message has been actually sent or not. If in some cases this doesn’t represent a big issue, it’s highly probable that in your application you want to be notified if some problem occurred during the operation.

At this point I haven’t found a reliable way for being notified about the result. Even using the startActivityForResult method of Ti.Android.currentActivity, instead of startActivity doesn’t help:

because the result value returned is always Ti.Android.RESULT_CANCELED.

Here is a complete example showing two alternative techniques for sending text messages through intents on Android with Ti Mobile.

At this point, for Ti Mobile applications that need to be notified about the result of the operation, the only solution would be to create an Android module that wraps the android.telephony.SmsManager class.

I’m currently experimenting around some basic code for such a module, so stay tuned ;-)