I have written the below code for sending SMS messages.
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(destAddr, null, mMessageText, il, null);
But this is not updating in my Inbox, I need to save the same message in Inbox,
Or is there any way to invoke a native SMS application to send SMS ?
You can use the sms content provider to read and write sms messages:
ContentValues values = new ContentValues();
values.put("address", "123456789");
values.put("body", "foo bar");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
I don't know why you would want to write a message you send to the inbox but if that is what you want just change the above uri to "content://sms/inbox"
.
Alternatively you can hand over to a messaging application by starting an activity with an intent similar to the following:
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms://"));
sendIntent.putExtra("address", "123456789");
sendIntent.putExtra("sms_body", "foo bar");
startActivity(sendIntent);
Edit: However, the sms://
content provider is not part of the SDK so I strongly recommend not using code like this in public applications for several reasons.
If you want to manually put some SMS to your inbox with a sender name then,
ContentValues values = new ContentValues();
values.put("address", "+923359110795");//sender name
values.put("body", "this is my text");
getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
also add these in manifest.
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
Now this code will add sms to inbox with a defined sender name, so you can easily handle you problem with this solution,
This code will work for all Android versions including above kitkat (19)
public boolean saveSms(String phoneNumber, String message, String readState, String time, String folderName) {
boolean ret = false;
try {
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", message);
values.put("read", readState); //"0" for have not read sms and "1" for have read sms
values.put("date", time);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Uri uri = Telephony.Sms.Sent.CONTENT_URI;
if(folderName.equals("inbox")){
uri = Telephony.Sms.Inbox.CONTENT_URI;
}
mActivity.getContentResolver().insert(uri, values);
}
else {
mActivity.getContentResolver().insert(Uri.parse("content://sms/" + folderName), values);
}
ret = true;
} catch (Exception ex) {
ex.printStackTrace();
ret = false;
}
return ret;
}
How to call
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final String myPackageName = getPackageName();
if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName);
startActivityForResult(intent, 1);
}else {
saveSms("111111", "mmmmssssggggg", "0", "", "inbox");
}
}else {
saveSms("111111", "mmmmssssggggg", "0", "", "inbox");
}
onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final String myPackageName = getPackageName();
if (Telephony.Sms.getDefaultSmsPackage(mActivity).equals(myPackageName)) {
//Write to the default sms app
saveSms("111111", "mmmmssssggggg", "0", "", "inbox");
}
}
}
}
}
For more detail or sample app follow link:
http://wisdomitsol.com/blog/android/sms/how-to-programmatically-save-sms-to-inbox-or-sent-in-android
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Content of the SMS goes here...");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
invoke a native SMS application with content
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", multimessage);
values.put("read", 1); //"0" for have not read sms and "1" for have read sms
Uri uri = Telephony.Sms.Sent.CONTENT_URI;
Activity ctx = this.cordova.getActivity();
ctx.getContentResolver().insert(uri, values);