5

In my application I need to read sms coming from only a number, and when i receive it I need to set it as read automatically, without setting it in the sms android application, but from my app. How can I do? Thanks!

pindol
  • 2,110
  • 6
  • 35
  • 52

2 Answers2

14

Let me update this:

ContentValues values = new ContentValues();
values.put("read",true);
getContentResolver().update(Uri.parse("content://sms/"),values, "_id="+SmsMessageId, null);

SmsMessageId is _id of message, which you find in SMS database.

Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
Michalsx
  • 3,446
  • 5
  • 33
  • 46
3

A short example:

Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
  // Retrieve sms
  // see column "address" for comparing

  // Then update the sms and set the column "read" to 1
}
hoferm
  • 209
  • 1
  • 4
  • Thanks for the help. I have a problem: `while (cursor.moveToNext() && !stato) { String asd = new String(cursor.getBlob(cursor.getColumnIndex("read"))); Toast.makeText(context, asd, Toast.LENGTH_SHORT).show(); }` If i set address in getColumnIndex there is no problem, but if i set read or other column name it doesn't work. Why? Thanks! – pindol May 19 '11 at 16:35
  • Last time I wrote something for android it's about a year ago. Sorry, but I don't know the answer. But there are several Uri's for the sms API. You can try to write the sms to a different one as **content://sms/inbox** and remove the old in the inbox. – hoferm May 20 '11 at 07:01
  • Can you post your solution? This would probably help other users with the same problem. – hoferm May 20 '11 at 11:19
  • String asd = cursor.getString(cursor.getColumnIndex("read")); i forget to post it, sorry – pindol May 20 '11 at 15:51
  • but after updating the read state from content provider default sms application still show this sms is unread – Asim Habib Mar 18 '14 at 05:53