How to get unread message in my SMS app android - android

I have created an SMS app for all versions. I am using the cursor adapter for showing the Message and number. Things are working fine except that I am not able to differentiate the new messages?
How to fix this SMS unread part? I would like to BOLD them when I receive it. But I don't know how what is received.
What I am doing now:
In my SMS receiver I store the new numbers to local DB and then compare them with the SMS content provider. This works just fine, but then takes a lot of time to load the messages.
How to fix this up?
Thanks!

I solved the problem with a few trial and errors..
Got the Read value for particular number and corresponding message( if "0" then bold them according).
To mark read I did the following:
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);
Hope it helps for people who are looking for solution...

Related

Android SMS how to only show latest message per contact and count conversation of a sender [duplicate]

I am trying to create an sms conversation list. I am using this code to get a cursor:
Cursor cursor = activity.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
The problem is that when I use the cursor, I get multiple messages from one contact. I am trying to get only the most recent message from all contacts so that I can show them in a recyclerview as a list of conversations.
Thanks in advance!
Querying the "content://sms/conversations" URI (Telephony.Sms.Conversations.CONTENT_URI) will return a summary of each conversation, with the "snippet" column (Telephony.Sms.Conversations.SNIPPET) being the last message in each.
This query will also return with a "msg_count" column (Telephony.Sms.Conversations.MESSAGE_COUNT) - which is pretty self-explanatory - and a "thread_id" column (Telephony.Sms.Conversations.THREAD_ID), which can be used to retrieve a complete conversation, by querying with that ID appended to the conversations URI. For example:
String threadId = ...
Uri convoUri = Telephony.Sms.Conversations.CONTENT_URI
.buildUpon().appendPath(threadId).build();
Do note that these queries will return only SMS messages. If you want MMS as well, Telephony.Mms and Telephony.MmsSms have similar URIs.

Android - Query All MMS & SMS from Specific Thread_ID

How can I query all messages sent by a specific address?
I couldn't find my answer on StackOverflow after many days of searching.
I'm currently getting all messages like this:
Uri uri = Uri.parse("content://sms/");
Cursor managedCursor = getContentResolver().query(uri, null, ("thread_id = " + ThreadID), null, "date ASC");
But this only returns SMS messages (no MMS). I tried changing content://sms/ to content://mms-sms/ but that was unrecognized. I also tried to query content://mms-sms/conversations/xxx where xxx is the thread_id but that just gave me a NullPointerException.
I've been searching for a solution to this for days. Other SMS apps such as Go SMS and Sliding Messaging can do it perfectly but I just can't seem to figure it out...
A null projection argument to content://mms-sms/conversations/xxx is what's causing the NullPointerException --- if you explicitly project the columns you want (e.g, {"address", "body", "date"}), it should work just fine.
However, querying by thread_id isn't strictly selecting based on the address the message is sent from; if you want that, you should probably look in content://sms/inbox (which ignores messages you've sent) and use selection arguments to query for the address you're interested in:
Uri uri = Uri.parse("content://sms/inbox");
Cursor managedCursor = getContentResolver().query(uri, null, "address=?", {targetedAddress}, "date ASC");

Reading a message in inbox from a particular contact

I want to read sms from my inbox. Here i want to read a specific message. Suppose mark has sent me a message i just want to make a query which will return the cursor indicating only Mark message .i have tried something like this but it is not working for me
Cursor cursor = contentResolverSender.query(
Uri.parse("content://sms/inbox"), null,"address =?"
,smsNo, null);

How to update android sms conversation thread?

I wanted to update the "message_count" column and "snippet" column of a sms conversation thread, using this content:"content://mms-sms/conversations". Because the snippet and message count is not updated when an SMS is deleted from the conversation thread.
But I got this error message: "MmsSmsProvider does not support deletes, inserts, or updates for this URI"
Below is my code:
ContentValues values = new ContentValues();
values.put("message_count", ent.getValue().getCount());
values.put("snippet", ent.getValue().getSnip());
Uri conUri = Uri.parse("content://mms-sms/conversations");
getContentResolver().update(conUri, values, null, null);
I also try like this:
Uri conUri = Uri.parse("content://mms-sms/conversations" + "/" + threadid);
getContentResolver().update(conUri, values, null, null);
But what I got is another error message:no such column: message_count: , while compiling: UPDATE pdu SET message_count=?,snippet=? WHERE thread_id=334
Thanks for any help
There is no way to update the count, snippet etc of a thread directly. But I can help you with your actual problem. There is actually a bug in the android code, which is the root of your issue. When you try to delete in the following way -
getContentResolver().delete("content://sms/", "_id=?", new String[] {id});
Android does not update the thread associated with that sms.
The solution is is to do this
getContentResolver().delete("content://sms/" + id, null, null);
For those who are curious what the bug is -
Android code does something stupid like this, when you delete using method 1 -
Delete all messages given by the query.
Get all the conversation threads associated with the messages given by the query and update them. Whoa wat ?! Step 1 just deleted messages given by query so, step 2 is always going to return a null set for threads associated with those message, as all the messages are already deleted.
Hope this helps.

Android: How to link sms draft with information about the receiver (like the name)?

if I write a new text message in the Android standard sms app and fill in a receiver and some body text and press the back button Android will save this as a draft. If the receiver is currently in my address book Android tells me also in the draft overview the name or, if the receiver is not in the address book, the number.
So far, so good.
Now I tried to do exactly the same thing on my own and I'm not quite sure why my solution doesn't work.
I've tried the following:
// in a Activity
final ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse("content://sms/draft"),
String[] {"_id", "date", "body", "person"},
null, null, null);
Later in my own cursor-Adapter I tried to get the person-id which links to the contacts table (I thought so). I do it this way:
// in my own CursorAdapter (bindView-Method)
Long receiverId = cursor.getLong("person");
But what I get is always "0". So what do I do wrong? Thanks in advance.
Btw: Is there any official documentation about the "content://sms"-thing? I only found some forum postings but nothing at developers.android.com!?

Categories

Resources