41

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 ?

Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
  • 8
    @Lo'oris, @ereOn: Programmatically means that you want do it "through code". It's a perfectly valid word, found in most dictionaries that have been printed in recent years. There are times it's necessary to distinguish between the occasional question we get from a user who is *not* attempting to do something through code. Those questions, of course, belong on Super User instead of here. The problem is that some tasks are much simpler to do and much more commonly done *without* writing a single line of code. The clarifier keeps people from providing those obvious answers. – Cody Gray - on strike Feb 17 '11 at 12:02

5 Answers5

60

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.

Josef Pfleger
  • 74,165
  • 16
  • 97
  • 99
  • 6
    The SMS content provider is not part of the Android SDK. Your code will break on devices that replace the SMS client with their own. Your code may break in future versions of Android. – CommonsWare May 29 '10 at 16:36
  • 4
    @CommonsWare: Do you know if this particular problem has an "official" solution ? Dealing with undocumented APIs indeed seems risky, but I couldn't find any other solution so far. – ereOn Feb 14 '11 at 14:04
  • Sorry to post on such an old question, but is there an alternative to using the sms:// cp? I am looking for this, but it seems to be the only way to do it. – Epicblood Dec 20 '13 at 00:24
  • I've implemented above mentioned code which will store message into inbox. but somehow I'm unable to find the stored sms. also i'm not even receiving any kind of error/exception. can someone help? thanks in advance – satyapol Apr 21 '15 at 09:42
28

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,

Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
8

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

Atif Mahmood
  • 8,882
  • 2
  • 41
  • 44
  • There was no "more information" at your link. It was a mere regurgitation of this non-explained answer (which itself seems like a copy of other long-since-posted answers). And yet again, you have included a nondisclosed link to your own site. I've removed the link. – Andrew Barber Feb 09 '13 at 05:46
-1
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

  • whats the difference on using content://sms/ ? I'm interested because I noticed that creating an unread SMS doesn't trigger any native notification. Will this way trigger some notification? – sports Apr 04 '14 at 21:31
-1
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);
Brad Larson
  • 170,088
  • 45
  • 397
  • 571