query the latest SMS messages group by contact - android

Like the Message app, the first Activity displays the lastest SMS grouped by person and the count number.
Example I had exchanged 50 texts with Chris, 60 with Aline, 40 with Ray ...
The app displays something like this
Chris (50) Hey how are you lately ?
Aline (60) Let's catch up
Ray (40) Here is my number
Ethan (1) I wrote a solution
I'm trying to do the same . I query all the SMS then I sort them.
At least it's O(n) efficient .
Cursor cur = this.getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
int count = cur.getCount();
// iterate all the SMS
for (int i=1 ; i<=count ; i++){
// processing
....
cur.moveToNext();
}
Is there a way to query the latest messages (received or sent no draft type) grouped by person and get the number of SMS from a person ?
I suppose there are multiple queries.

I think this will do what you want:
Cursor cur = this.getContentResolver().query(Uri.parse( "content://mms-sms/conversations?simple=true"), null, null, null, "normalized_date desc" );
The returned cursor will contain the latest message in every conversation and the amount of messages in each conversation.

Related

How to get the number of already read messages in android?

In my app,i want to give the user info about the number of already read and unread messages from inbox.I have searched and found out that there is no such content://sms/read is present in android api.I have tried this code till now:
Uri number_of_sms_read = Uri.parse("content://sms/read");
Cursor c = ShowTheMessages.this.getContentResolver().query(number_of_sms_read, null,null, null, null);
c.moveToFirst();
NumberOfSmsReadInInbox = c.getCount();
Can you please tell me is it possible to get the count of read and unread messages in android?
Thanks in advance.
I suppose you can take a look at this question which is the reverse of yours: Get number of unread sms
It provides the WHERE condition "read = 0" to the query, so you might want to supply a "read = 1".
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(SMS_INBOX, null, "read = 1", null, null);
int unreadMessagesCount = c.getCount();
c.deactivate();

How do I get the count of SMS messages per contact into a textview?

I have a listview that displays the contacts on my device. What I'm trying to do is display the number of text messages my device has received from each contact into a textview within my listview. I have only been able to display the total number of text messages within my inbox from this code:
// gets total count of messages in inbox
String folder = "content://sms/inbox";
Uri mSmsQueryUri = Uri.parse(folder);
String columns[] = new String[] {"person", "address", "body", "date","status"};
String sortOrder = "date ASC";
Cursor c = context.getContentResolver().query(mSmsQueryUri, columns, null, null, sortOrder);
textview.setText(c.getCount());
The problem with the above code is that for every row in my listview, this only shows the total. How can I split the total number between to it's corresponding contact?
The end result is like this if I had 100 messages in my inbox:
Contacts:
Foo Manchuu: 25
Bar Bee: 15
Sna Fuu: 10
John Doe: 50
Uri SMS_INBOX = Uri.parse("content://sms/conversations/");
Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null);
startManagingCursor(c);
String[] count = new String[c.getCount()];
String[] snippet = new String[c.getCount()];
String[] thread_id = new String[c.getCount()];
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
count[i] = c.getString(c.getColumnIndexOrThrow("msg_count"))
.toString();
thread_id[i] = c.getString(c.getColumnIndexOrThrow("thread_id"))
.toString();
snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet"))
.toString();
Log.v("count", count[i]);
Log.v("thread", thread_id[i]);
Log.v("snippet", snippet[i]);
c.moveToNext();
}
See log and problem solved. Dont repost the same question : How do I display the count of text messages received from a contact?
I would perhaps add count(person) AS cnt to the columns String array and then I would add person as the groupBy argument to the query method.
EDIT:
And as pointed out by Jens, the GROUP BY paradigm isn't supported by the messaging ContentProvider.
What you could do is to create an in-memory database of your own at application startup (http://www.sqlite.org/inmemorydb.html) and copy-paste (query-insert) messages of interest from the system messaging database into your in-memory database. (NOTE! That this might fall out wrong if you are low on memory and have a tremendous amount of messages you're interested in). Since you'd be God in the in-memory database you could query it as you see fit with any exotic SQL querys you'd like...

Android Record Count Toast Message?

I have an App I would like to cripple by allowing only 5 records to be shown when doing a query.... The following is the code I'm using within the DataBaseHelper.java for this and it works great for what I need........It returns only 5 records in DESC order by KEY_RECORD1
My question is I would like implement a Warning message in the form of a Toast when this 5 Record limit it reached.....
So when you Add the Sixth Record you would get....Example Toast: Limit of 5 Records has been reached!
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE,
new String[] { KEY_ROWID, KEY_RECORD1, KEY_RECORD2,
KEY_RECORD3}, null,
null, null, null, KEY_RECORD1 + " DESC", "5");
}
if (fetchAllNotes().getCount() >= 5){
//Toast.makeText...
}
to avoid extra request and improve perfs, you could do that once, to retrieve the number of records, and increment that counter each time you do an insertion.

how to get phone numbers for particular group

I'm working on Android contact. I want to query phone numbers (not contact name) from a specific group name. What query should i perform in order to do this?
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI
, null, ContactsContract.Data.MIMETYPE+"=?"
, new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE}
, null);
Then loop through cursor and get data u want. This will return data blocks showing the contactID and the groupID and other info. With this then query ContactsContract.Groups and get data about the group to compare.
If you are looking for specific data about a group first query for group row ID than you can add that to the following cursor like so...
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI
, null, ContactsContract.Data.MIMETYPE+"=? AND "+ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+"=?"
, new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, rowID }
, null);
wrote code here so sorry for silly mistakes
You can find group id like so...
Cursor c = context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, new String[]{ContactsContract.Groups._ID}, ContactsContract.Groups.TITLE+"=?","myGroup", null);

For Android, is there a way to determine how many text messages have been sent to a contact?

I'm reading contact data from ContractContacts, and can lookup TIMES_CONTACTED (which is useful to me), but this field only applies to calls to that contact. I'm also interested in the number of times a contact has been contacted via SMS or email.
Does anyone know if this information is available? I've been searching but haven't come across anything.
For SMS, you'll want to access the inbox located at content://sms/inbox directly and perform a database query to count the number of rows corresponding to the matching contact.
Something like:
String personAddress = addressFromContact();
Uri smsUri = new Uri("content://sms");
if (smsUri != null) {
Cursor smsCursor = getContentResolver().query(smsUri, null, "address=?", new String[] {personAddress}, null);
int smsCount = smsCursor.getCount();
}

Categories

Resources