Over a million developers have joined DZone.
Platinum Partner

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

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

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.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}