Marcin Świerczyński is a software developer especially interested in Java and Python languages, Java-based technologies and mobile applications development. He strongly believes in Software Craftsmanship as a way to build reliable and maintainable software. Marcin has posted 3 posts at DZone. View Full User Profile

How to Add a Text Message to a Messages Conversation in Android SDK

  • submit to reddit
Today I've published a new version of my AutoResponder Android application. The main feature in this release was quite simple: show auto-sent messages within standard messages conversation, just as if they were sent by hand. So, how do you do this in the Android SDK? Because it is a background operation, not visible to user, I've decided to build a Service to accomplish this task.

public class SentSmsLogger extends Service {

private static final String TELEPHON_NUMBER_FIELD_NAME = "address";
private static final String MESSAGE_BODY_FIELD_NAME = "body";
private static final Uri SENT_MSGS_CONTET_PROVIDER = Uri.parse("content://sms/sent");

public void onStart(Intent intent, int startId) {

private void addMessageToSentIfPossible(Intent intent) {
if (intent != null) {
String telNumber = intent.getStringExtra("telNumber");
String messageBody = intent.getStringExtra("messageBody");
if (telNumber != null && messageBody != null) {
addMessageToSent(telNumber, messageBody);

private void addMessageToSent(String telNumber, String messageBody) {
ContentValues sentSms = new ContentValues();
sentSms.put(TELEPHON_NUMBER_FIELD_NAME, telNumber);
sentSms.put(MESSAGE_BODY_FIELD_NAME, messageBody);

ContentResolver contentResolver = getContentResolver();
contentResolver.insert(SENT_MSGS_CONTET_PROVIDER, sentSms);

public IBinder onBind(Intent intent) {
return null;


SentSmsLogger expects an Intent with receiver number and message body. Then it passes that information to proper ContentProvider. And this is the clue - it isn't well documented how to manage ContentProvider associated with messaging module. Google to the rescue ;) I've found information that relevant the ContentProvider has the URI content://sms/sent.

The next step was to find out the names of the fields that contain data about message body and its receiver. With the help of debugger, I've found them - it's body and address. I've put all of this in private constants at the top of class. That's all! Now we can use this data in a standard manner. Useful information about this can be found in the official documentation. Important note: due to compatibility issues, I've used Android SDK v. 1.5 here.

Your rating: None
Published at DZone with permission of its author, Marcin Świerczyński.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Jesse Sightler replied on Sun, 2010/07/25 - 10:20pm

I may be wrong, but I believe the contentprovider you are using is not specified to use a standard structure in android. Ie, this may change between versions and possibly even between vendors of the same version, as the behaviour here is not a published/public API. I could be mistaking this for something else, though. :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.