Reading SMS in android, showing wrong count - android

I am trying to read sms in android. In my manifest file, I have:
<uses-permission android:name="android.permission.READ_SMS" />
My Code:
Cursor cursor = getContentResolver().query(Uri.parse("content://mms-sms/inbox"), null, null, null,null);
((TextView) findViewById(R.id.info1)).setText("Number of messages: " + cursor.getColumnCount());
The problem is that I have more than 17 smses in my inbox but the output it gives is
Number of messages: 17
I have looked at this, this, this and this. Can someone please tell what the problem is?

You are asking for the column count:
cursor.getColumnCount()
Which is very different from what you want:
cursor.getCount()
getCount() returns the number of rows in a Cursor, or in this case the number of messages in your inbox.
getColumnCount() returns the number of columns in each row; like id, sender, message, time, etc. Read the second answer here: How many database columns associated with a SMS in android?

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.

How to get only most recent SMS to use in conversation view?

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.

How to make difference between read and unread message

In my app I need to get sms from specific number to activity.I got it.Now how could I differentiate read and unread messages of that specific number.And also I need to show unread message count out off total messages from that number in my previous activity.Can anyone tell me how can I accomplish this task?I tried the following code,
Cursor cur = getContentResolver().query(uri,new String[] { "_id", "thread_id",
"address", "person", "date","body", "type" },"read=0", null, null);
But this return 0 rows in listview.I also tried to get the total number of messages by using
msgcount=list.size();
How can I get the msgcount value to my previous activity. I declared the variable as public static. But it shows 0 while the listsize is 11.
Please Help!
For unread message read=0 for more details follow this link Get number of unread sms
You can add sms_status flag as a column , it's default value 0 .
when you will finish to read sms ,update flag and value will be 1.
when you will query unread sms then check sms_status flag 0 and phone number
msgcount=list.size();
if(msgcount > 0){
for(SMS sms:list){
String smsFlag = sms.getSmsFlag();
if(smsFlag .equals("0")){
// read sms
}else{
//unread sms
}
}
}

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.

How do I sort SMS and MMS together?

I'm implementing the methods discussed here: How to Read MMS Data in Android?
I'm trying to read SMS and MMS into a single listview. I'm doing pretty well, but when I try to sort I'm getting all of the SMS sorted together followed by all of the MMS sorted together.
Here is my code:
Cursor smsCursor = getContentResolver().query(Uri.parse("content://mms-sms/conversations/"), null, null, null, "date DESC");
Can anyone tell me how to combine the two sources or how to combine the MMS into the SMS conversations like the built in Android app does?
Edit: I noticed that the date of SMS has several more digits than the dates of MMS.
Edit 2: Adding "julianday()" like so:
Cursor smsCursor = getContentResolver().query(Uri.parse("content://mms-sms/conversations/"), null, null, null, "julianday(date) DESC");
Makes the MMS appear at the top of the list.
try normalized_date desc instead of date desc for the order.
It should work.
The real problem here seem's to be that in the Database, the date is not stored exactly the same Way for SMS and MMS.
So you'll need to first query the database looking just for the "date" field.
(Not normalized_date that would crash on some devices).
Then sort your list programmaticly in Java, taking in account that for the MMS,
the date needs to be multiplied by 1000 as stated here :
How do the retrieve the date of the mms from content://mms.*
Or here
mms sent/recive date is always in 1970

Categories

Resources