Sending Text Messages with Body Text using AIR for Android - Hackish Workaround

Posted on Feb 09, 2011

Sending Text Messages with Body Text using AIR for Android - Hackish Workaround

It's been a while since I posted actual code samples here, in part because I have been working with prerelease versions of the software and SDK that are still technically NDA (on that note, if you'd like to get into the Flash Builder "Burrito" and Flex "Hero" SDK prerelease program, you can find out how to apply here). I promise I will try to have more code in upcoming posts.

In today's post I want to cover sending text messages within your AIR for Android application. As others have pointed out, the basics are very simple. You simply use navigateToURL(new URLRequest("sms:6175551212")) and the text messaging application will open with the number you have specified already pre-entered. However, in most cases, this just simply isn't very useful for two reasons. Reason one is that there is no integration with the address book, so in many use cases, having the user actually type the number within your application isn't very helpful (as a side note, you can actually do something like navigateToURL(new URLRequest("sms:Brian Rinaldi")) but it's not much of an improvement in my opinion). The second reason is that, unlike the email integration, you cannot specify a body for the message. After doing a bit of research, it appears that is a limitation within Android at the moment and not within AIR. So, while it's far from ideal, here's how I am working around this limitation - and I encourage you to offer better solutions in the comments section if you have ideas.

To deal with the address book limitation, I decided not to even have the user enter the phone number within the application since the messaging applicaiton within Android does pull addresses from the address book when entering the to field. To do this, you simply call the sms URL with no "to" info like navigateToURL(new URLRequest("sms:")). This will open the messaging application with the "to" field empty as you'd expect. This to me seemed preferable to the alternative of actually specifying a name or number within my application without address book integration.

To address the lack of support for specifying the message body in Android, I looked to AIR for Android's ability access the device clipboard using Clipboard.generalClipboard. My idea was that when you click the "Send Text" button, I would copy the text to the clipboard allowing you to paste it into the body of your text message. At first, I just put a label within my View that stated that this would occur but, honestly, I think it was far from intuitive given that many users would likely not read the message. Instead, I thought an alert would work better but that hit yet another limitation - there is no Spark or mobile version of the mx Alert (yet). Not wanting to bring in a bunch of Halo dependencies, I did some research and landed upon this forum post by a community member named Darren. In it he shares a version of an Alert box he created. I utilized that message box within my application, just removing the image and icon dependencies for simplicity's sake (though I'm sure his looks better than mine).

Finally, let's look at some code. The following function is called when I click on the "Send Text" button within my application. It simply opens the alert box notifying the user that the message text is being copied to their clipboard so that, hopefully, they can figure out to paste it into the SMS body field:

protected function showMessage():void {
   var msgBox:MessageBox   = MessageBox.show(this, MessageBox.MB_OK, "Note", "The message has been copied to your clipboard.");
   msgBox.addEventListener(MessageBoxEvent.MESSAGEBOX_OK, sendMessage);
}

You'll notice above that the callback function when the user clicks the "OK" button in my alert box is sendMessage(). That function actually does the copying of the text to the clipboard (the text being contained in a TextArea with the ID of translatedLine - as a side note, this application uses the Google Translate API which I hope to discuss in a future post). It then calls the messaging application as we discussed above, without any "to" information.

protected function sendMessage(event:MessageBoxEvent):void {
   navigator.popToFirstView();
   Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT,translatedLine.text)
   navigateToURL(new URLRequest("sms:"));
}

As I stated earlier, this is far from the ideal workflow I wanted for the user within my little applicaton. Nonetheless, it is the best alternative to actually having the ability to specify the SMS body text that I could come up with. I'd love to hear from others of you who might have suggestions for improvement or alternative workflows. Has anyone out there run into this issue? If so, how did you choose to work around it?

Comments

There are currently no comments for this entry...be the first!

Write your comment



(it will not be displayed)





About

My name is Brian Rinaldi and I am the Web Community Manager for Flash Platform at Adobe. I am a regular blogger, speaker and author. I also founded RIA Unleashed conference in Boston. The views expressed on this site are my own & not those of my employer.