Restore SMS in inbox - android

I want to restore sms in phone, but code seems to have no effect.
I read sms, and get sms from thread_id number 4, then I want to add sms in this conversation, using those lines :
ContentValues initialValues = new ContentValues();
initialValues.put("address", "0644552211");
initialValues.put("date", String.valueOf(System.currentTimeMillis()));
initialValues.put("body", "test test");
initialValues.put("type", 1);
initialValues.put("thread_id", 4);
initialValues.put("read", 1);
getContentResolver().insert(Uri.parse("content://sms/inbox"), initialValues);
But nothing seems to be inserted.
EDIT
insert(...) method return that : content://sms/inbox/0

Answer found :
insert(...) method always return : content://sms/inbox/0
because my app was not default sms app !

Related

Saving sms was sent from my app into db of sms app native

I search more topic same my problem in stackoverflow and google but all solution can't solve my problem. I want when I send sms from my app, it also will store in db so sms native can read.
Could people can guide or give me example code to save sms into db sms.
Thanks so much!
Note: I also use SMSManager and insert into content URI content://sms/sent but don't succes.
Update the correct answer:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 16) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(address, null, body, null, null);
} else {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(address, null, body, null, null);
ContentValues values = new ContentValues();
values.put("address", address);//sender name
values.put("body", body);
context.getContentResolver().insert(Uri.parse("content://sms/sent"), values);
}
Look how it works. Checkout this app from github - specifically look into MessageDAO.java
Also keep in mind that starting KitKat in order to have write access to SMS db application need to be declared as default SMS handling app.
Look into PSMActivity.handleDefaultSMSApplication
You can use ContentProvider with URL "content://sms/inbox" and "content://sms/sent"
Then insert your sms here!
An example for you:
ContentValues values = new ContentValues();
values.put("address", "+84935059109");//sender name
values.put("body", "textttttttt");
getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
Remember to declare in manifest:
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>

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

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

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.

Android: Updating the sent box afer sending an sms

My app sends an sms and I would like to update the phone sent box
as if the sms was sent bu the user.
How can this be done ?
You need to add this lines after smsManager.sendTextMessage(number, null,desc, sentPI, deliveredPI);:
ContentValues values = new ContentValues();
values.put("address", number);
values.put("body", desc);
getApplicationContext().getContentResolver().insert(Uri.parse("content://sms/sent"), values);

How can I send fake sms to myself without mobile network usage?

I want to show some information as an sms in my application.
But SmsManager and BroadcastReciever can not create sms and notify on phone itself.
How can I send a fake sms to myself programmatically? Any ideas, workarounds or any class for research... ?
Thanks.
PROOF OF CONCEPT
Call SMS list
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(
new ComponentName("com.android.mms","com.android.mms.ui.ConversationList"));
startActivity(intent);
READ SMS
cursor c= getContentResolver().query(uri, null, null ,null,null);
startManagingCursor(c);
c.moveToFirst();
String body = c.getString(c.getColumnIndexOrThrow("body")).toString();
String number = c.getString(c.getColumnIndexOrThrow("address")).toString();
c.close();
Toast.makeText(getApplicationContext(), number, Toast.LENGTH_LONG).show();
Write SMS
ContentValues values = new ContentValues();
values.put("address", "SENDER");
values.put("body", "foo bar");
getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
MANIFEST
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
ISSUE
There is no event after that therefore android doesnt known if there is some new message.
You can send SMS from another instance of Android Emulator. That could be done through Emulator control View in Eclipse or using Telnet.

Categories

Resources