How to get the number of already read messages in android? - 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();

Related

Retrieve SMS sent after a specific time

I have a background service that listens to changes to Android SMS content provider. I store the last recorded id of the message that my Service recorded and using it, I want to find the number of messages that were sent when the service was Stopped/Killed.
So when the service is started again and another message is sent, I want to use the timestamp of the last recorded message. I use the below code to do this. My problem is that the cursor always returns 1 record irrespective of how many messages are sent when the service was killed before its restarted.
String COLUMN_NAME_DATE = "date"
String[] projection = new String[]{COLUMN_NAME_ID, COLUMN_NAME_DATE};
Cursor newMessagesCursor = mContext.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_SENT),
projection,
COLUMN_NAME_DATE + "> ? ",
new String[]{String.valueOf(lastMessageTimeStamp)}, null);
int newMessagesCount = (null != newMessagesCursor) ? newMessagesCursor.getCount() : 0;
Any pointers would be helpful. Let me know if there is any alternative approach as well.
Edit: Adding code to retrieve the lastMessageTimeStamp.
Cursor messageCursor = mContext.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_SENT),
projection,
COLUMN_NAME_ID + "= ? ",
new String[]{ lastMessageId}, null);
if(null != messageCursor && messageCursor.moveToFirst()) {
Long lastMessageTimeStamp = Long.parseLong(messageCursor.getString(messageCursor.getColumnIndex(COLUMN_NAME_DATE)));
}
Complete Source code for this class is located at:
https://github.com/midhunhk/message-counter/blob/unicorn/messageCounter/src/main/java/com/ae/apps/messagecounter/observers/SMSObserver.java
I was able to solve this issue by using a different approach.
It seems like when we query the table SMSManager.SMS_URI_SENT as in the question, it is only returning 1 row which is the last message that was sent.
Cursor newMessagesCursor = context.getContentResolver().query(
Uri.parse(SMSManager.SMS_URI_ALL),
MessagesTableConstants.SMS_TABLE_PROJECTION,
"person is null and " + MessagesTableConstants.COLUMN_NAME_DATE + "> ? ",
new String[]{String.valueOf(lastMessageTimeStamp)}, null);
I query for SMSManager.SMS_URI_ALL and check for person column as null which represents messages that are sent.

Unable to access Android content provider

I can see in LogCat that ActivityManager is reporting something about a particular content provider. I use the following code to try to access that content provider in order to learn more about it:
Cursor cursor = context.getContentResolver().query(uriDSDS, null, null, null, null);
String[] columnNames = cursor.getColumnNames();
Log.d(TAG, "columnNames=" + columnNames.toString());
Unfortunately, after trying to get the cursor, I see the following error in LogCat:
Failed to find provider for __
What's the problem? Why can I not access this provider? Could it be that access is restricted to certain apps? Is there a way to go into ADB or something else to see all of the available content providers on the device?
make sure that you add correct Uri
for example
Uri uriDSDS = Uri.parse(
"content://aaa.aaa/values");
As in the working example below, do check if your uriDSDSis correct.
Cursor people = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
// get contact id
while (people.moveToNext()) {
if (people != null) {
int numberFieldColumnIndex = people
.getColumnIndex(PhoneLookup._ID);
String number = people.getString(numberFieldColumnIndex);
contactId.add(number);
}
}
people.close();

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...

How to fetch the contact details from recepient_ids in threads table

This is the 'threads' table instance
_id| recipient_ids| snippet
1 | 1 |Hi this is hello world
2 | 2 |Multiple send
3 | 1 3 4 |Send
The values corresponding to the recepient_ids are placed in 'canonical_addresses' table
_id| address
1 |9879565655
2 |1111111111
3 |5465321348
4 |8965321354
Now i have to fetch the 'canonical_addresses.address' for each 'threads.recipient_ids' present in threads table (At times recipient_ids can be more than one) ?
Note:
I'm using content://mms-sms/conversation to fetch details from threads table.
you need to formulate the query as per the below:
String [] colAddress={"DISTINCT address"};
Cursor cur = getContentResolver().query(uriSMSURI, colAddress, null, null,null);
I couldnt find a direct solution towards it.
Indeed, in order to get the recipient details query Content://sms/ with the thread id.
select distinct address from the cursor.
We will be able to get the recipient details.
For more efficient ways pls respond to it.
The snippet is as follows
Uri THREAD_ID_CONTENT_URI = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(uribuilder.build(), new String[]{"address"},"thread_id=#) group by (address", null,null);
Thank u
Use this uri for fetching
ContentResolver cr = context.getContentResolver();
Cursor pCur = cr.query(
Uri.parse("content://mms-sms/canonical-addresses"), new String[]{"address"},
"_id" + " = ?",
new String[]{your_thread_id}, null);
String contactAddress = null;
if (pCur != null) {
if (pCur.getCount() != 0) {
pCur.moveToNext();
contactAddress = pCur.getString(pCur.getColumnIndex("address"));
}
pCur.close();
}

Reading text from SMS, and show it as text view

I am trying to create app, that will get the text from SMS, and use it in textview. So something like this, message is recived, i check if it is message I want, then i extract text, save it to string, and then show this string in textview. Any suggestions from where should i start, any examples plese ??
You can start here for handling received SMS.
First I would listen for SMS incoming, and on incoming SMS show a notification. Then if the user opens your app, update your display using this to get the data you want:
Uri allMessage = Uri.parse("content://sms/inbox");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
//shows one message
c.moveToNext();
//uncomment to cycle thru ALL messages... This will take AWHILE
//while (c.moveToNext()) {
for(int i = 0; i != c.getColumnCount(); i++){
String columnName = c.getColumnName(i);
String columnValue = c.getString(i);
Log.v(TAG, "Col: " + columnName);
Log.v(TAG, "Val: " + columnValue);
}
//}
Play around with it a little. It Should have all of the data you need (distinguish SMSs by timestamp)

Categories

Resources