Updating sent SMS in default app thread - android

I have developed an app in which I send an SMS upon a certain event. I have experimented with the
following two methods:
Method 1:
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(srcNumber, null, message, null, null);
Method 2:
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + srcNumber));
intent.putExtra( "sms_body", message );
startActivity(intent);
My requirements are:
1) The SMS should be sent without opening the phone's default SMS app.
2) The sent SMS should appear in the message thread in the default message app.
Method 1 satisfies requirement 1 and Method 2 satisfies requirement 2. Is there any way to satisfy both my requirements?

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(srcNumber, null, message, null, null);
ContentValues values = new ContentValues();
values.put("address", srcNumber);
values.put("body", message);
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
This should solve it.
NOTE: This requires android.permission.WRITE_SMS permission

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"/>

Restore SMS in inbox

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 !

Android Write to default SMS App

My Android App is using the following code to send an SMS Message:
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(smsMessage.getNumber(), null, smsMessage.getText(), sentPI, deliveredPI);
However, is it possible to save the outgoing message so that it is displayed in the default SMS app, just as if the user used the default app to send it?
Thanks
you must insert your smsMessage into default SMS_database
like this
public void insertSMS(String address,String body){
private ContentResolver resolver = null;
resolver = context.getContentResolver();//context is your instance of Activity
ContentValues values = new ContentValues();
values.put("address", address);
values.put("body", body);
resolver.insert(Uri.parse("content://sms/sent"), values);
}

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