How to make difference between read and unread message - android

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

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 unread message in my SMS app 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...

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);

Reading SMS in android, showing wrong count

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?

Getting phone number in inbox/outbox

I am currently using
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://sms/");
Cursor messagesCursor = cr.query(uri, new String[] { "_id", "address", "body", "person" }, null, null, null);
if (messagesCursor.getCount() > 0) {
while (messagesCursor.moveToNext()) {
colName = getContactName(messagesCursor.getString(messagesCursor.getColumnIndex("address"))) + "\n";
}
}
to get all the text in the user's inbox. From there, messagesCursor.getColumnIndex("address") will get the SMS's number. Then it will be place in getContactName() where it will then get the contacts' name from their contact's list.
This method will take a long time as it goes though EVERY SINGLE text message, get number, then get the contact's name, check if name exist in array, if it doesn't put in array then from there create a ListView.
The app will take a long time to load on start up if the user's inbox contains lots of text.
Is there any special way to get UNIQUE numbers from inbox? Something in cr.query?
Thanks in advance.(:
EDIT :
I'm creating just a simple inbox application. It reads from the original inbox message and list the messages in it.
For this project, what I did was to read through ALL the "address" which is the phone number, put it in an ArrayList, and finally to the ListView. As it cycles through all the messages in inbox, it takes a long time to startup.
For example :
Inbox Messages
Person 1
Person 1
Person 1
Person 2
Person 2
Person 3
Person 3
Person 3
Person 3
What my current code does :
Read EVERY SINGLE message
Check if name in ArrayList
If it is, skip. If not, add in array.
Throw ArrayList into ListView.
What my intention was :
A code that only reads out :
Person 1
Person 2
Person 3
Try only using "address" in projection (that means, don't include even "_id") - I'm not sure about this but it may solve the case.

Categories

Resources