Write sent sms to content://sms/sent table - android

I am working on an android sms application.I can send sms to single contact by using the following code.
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
Now I want to send sms to multicontacts.Some suggest to use loop.SO now I am using loops to send sms to multicontact.
After sending each sms I write those values to sent table.
ContentValues values = new ContentValues();
values.put("address", mobNo);
values.put("body", msg);
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
Every new address will create a new thread id.
For example if my receiver's address is x, then thread id 1, for y thread id 2.And if I want to send sms to both x and y ,then how can I write in to sms/sent table.
If I use Loop,then it won't create any new thread id, because send address x already have thread id 1 and y already have thread id 2.So messages will listed under thread id 1 and 2 never creates a new thread id.
I tried to manualy insert thread id by
values.put("thread_id", 33);
But then the messages under new thread id do not listed in default app but in my app.
Please help me friends
Edit:I tried using 0, and then reading the thread_id that was generated, then place the next sms with this thread_id, still doesn't works.

You need to create a new thread_id manually, a normal contentResolver.insert(...) won't do for multiple recipient messages. To create the new thread_id you query the following uri
content://mms-sms/threadID
and to it append the necessary recipients so that finally it looks like this
content://mms-sms/threadID?recipient=9808&recipient=8808
So the full example would look like this. Say the recipients are 9808 and 8808
Uri threadIdUri = Uri.parse('content://mms-sms/threadID');
Uri.Builder builder = threadIdUri.buildUpon();
String[] recipients = {"9808","8808"};
for(String recipient : recipients){
builder.appendQueryParameter("recipient", recipient);
}
Uri uri = builder.build();
Now you can query uri in the normal way and this will give you a thread_id that you can use for the recipients specified, it will create a new id if one doesn't exist or return an existing one.
Long threadId = 0;
Cursor cursor = getContentResolver().query(uri, new String[]{"_id"}, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
threadId = cursor.getLong(0);
}
} finally {
cursor.close();
}
}
Now use threadId to insert your SMSs.
A few things to note.
Do not use this threadId to insert single recipient messages for either 9908 or 8808, create a new thread_id for each or just do an insert without specifying the thread_id.
Also, be very careful with the builder.appendQueryParameter(...) part, make sure the key is recipient and not recipients, if you use recipients it will still work but you will always get the same thread_id and all your SMSs will end up in one thread.

Looks like you should create a new thread for the group message and insert it into the new thread as well as the individual threads.

Related

Android SMS not displayed even after inserting into database correctly

I am developing an SMS backup and restore app. I am able to backup the SMSs into a CSV file. However, when I try to restore the SMSs, the SMSs are stored perfectly into the SMS database. But they are not visible into the Messaging App in Android.
I am using the Content Provider for backup and restoring the SMSs. Following is the code I am using for Restoring the SMSs.
CSVReader reader = new CSVReader(new FileReader(csvFile));
Uri uri = Uri.parse("content://sms/");
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
ContentValues cv = new ContentValues();
cv.put("_id", nextLine[0]);
cv.put("thread_id", nextLine[1]);
cv.put("address", nextLine[2]);
cv.put("person", nextLine[3]);
cv.put("date", nextLine[4]);
cv.put("protocol", nextLine[5]);
cv.put("read", nextLine[6]);
cv.put("status", nextLine[7]);
cv.put("type", nextLine[8]);
cv.put("reply_path_present", nextLine[9]);
cv.put("subject", nextLine[10]);
cv.put("body", nextLine[11]);
cv.put("service_center", nextLine[12]);
cv.put("locked", nextLine[13]);
this.getContentResolver().insert(uri, cv);
}
Please Let me know, what mistake I am doing. I am trying to work things out for past 1 week, still I don't know my Mistake in the Code. What am I missing in the code?
Do not include _id and thread_id in your inserts. After all records are inserted, do the following to trigger the conversations table threads update:
getContentResolver().delete(Uri.parse("content://sms/conversations/-1", null, null);

query the latest SMS messages group by contact

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.

Android: null string issue for 'address' column for sms content provider

I am working on a android application which read the sms from content provider. The application works fine and read sms fine from content provider. But sometime (very rare) the 'address' column returns null for the sms message.
Here is sample code What I am using:
String whereClause = "_id > " + String.valueOf(Database.getLastSmsId(this));
Cursor cursor = getContentResolver().query(smsUri, null, whereClause, null, null);
if(cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
String body = cursor.getString(cursor.getColumnIndex("body"));
String address = cursor.getString(cursor.getColumnIndex("address")); // <----- Here is the problem
// address returns as null string
String date = cursor.getString(cursor.getColumnIndex("date"));
Log.d(Constants.TAG, "SMS event received. address="+address);
} while(cursor.moveToNext());
}
I am getting this issue on Motorola Driod Android v2.3.5. Please advise.
Ali
I found the problem.
The android OS save the message as draft in SMS content provider, if start writing and then close the SMS app (due to incoming call or any other reason). This generate a new entry in SMS database with null value in 'address' column.
Salman
This is a normal behavior. Found in the SmsMessage documentation:
Returns the originating address (sender) of this SMS message in String
form or null if unavailable
If your app breaks because of a NPE later, you should insert a null check and set the address to an empty string.
An advice: A question should asked something and you should provide a stack trace if you have an error.
This can happen in case of masked number.

Set sms as read in Android

In my application I need to read sms coming from only a number, and when i receive it I need to set it as read automatically, without setting it in the sms android application, but from my app. How can I do?
Thanks!
Let me update this:
ContentValues values = new ContentValues();
values.put("read",true);
getContentResolver().update(Uri.parse("content://sms/"),values, "_id="+SmsMessageId, null);
SmsMessageId is _id of message, which you find in SMS database.
A short example:
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
// Retrieve sms
// see column "address" for comparing
// Then update the sms and set the column "read" to 1
}

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