Android tutorial: mark SMS as read / unread

7 09 2013

When your app uses SMS inbox, you may want to be able to mark particular message as read or unread. SMS inbox has a property assigned to each message that indicates whether message is read or unread. You can access this property by ContentProvider. Here is how to do it:

1. Require WRITE and READ SMS permission

First of all to read and change inbox messages properties you must declare permission in AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

2. Having SMS sender and body, mark SMS as read

You simply need to search for the message in inbox and update it. Do it by using ContentResolver, providing proper projection and selection arguments:

public void markSmsAsRead(final String from, final String body) {

	Uri uri = Uri.parse("content://sms/inbox");
	String selection = "address = ? AND body = ? AND read = ?";
	String[] selectionArgs = {from, body, "0"};

	ContentValues values = new ContentValues();
	values.put("read", true);

	context.getContentResolver().update(uri, values, selection, selectionArgs);
}

The code above accesses the SMS Inbox content and finds messages received from sender (address = from), with specified content (body = body) and unread (read = 0). Then it updates their status to read = 1 (read = true). That’s all. It’s optimal and efficient.

Note

If you want to mark SMS as read right after it was received then you have to wait few seconds until it gets stored in SMS database. I do it by sleeping a thread for 3 seconds before searching Inbox. This is how it looks like:

public void markSmsAsRead(final String from, final String body) {

	Thread waiter = new Thread(new Runnable() {

		@Override
		public void run() {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				Log.w(TAG, "Exception while sleeping markSmsAsReadThread: " + e.getMessage());
			}

			Uri uri = Uri.parse("content://sms/inbox");
			String selection = "address = ? AND body = ? AND read = ?";
			String[] selectionArgs = {from, body, "0"};

			ContentValues values = new ContentValues();
			values.put("read", true);

			int rowsUpdated = context.getContentResolver().update(uri, values, selection, selectionArgs);
			Log.i("TAGGG", "rows updated: " + rowsUpdated);
		}
	});
	waiter.start();
}

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

Advertisements

Actions

Information

3 responses

10 02 2015
tariqul00

this code is not working, my phone specification kitkat 4.4.2 is there other way

20 02 2015
Anonymous

It might be related to the latest changes in android kitlkat http://android-developers.blogspot.ru/2013/10/getting-your-sms-apps-ready-for-kitkat.html

28 05 2016
Syed Fahad Khalid

add “Context context” parameter to markSmsAsRead().

Give Your feedback:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: