Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save SMS to inbox in android?

Tags:

android

sms

People also ask

Can SMS messages be saved?

Android - Quick steps to forever save text messagesLaunch SMS Backup+ and select Connect. Accept the prompt to Backup. All your text messages will start backing up to Gmail and can save them forever.

How do I save text messages on my Samsung Android?

After your device is linked to your Samsung account, go to its Settings > Cloud & Accounts and select Samsung Cloud. Now, under the backup settings option, you can enable/disable the sync of various data types. To do Samsung messages backup, just toggle on the “Messages” feature here and tap on the “Sync Now” button.

How do I save my SMS to Gmail?

To get started, first enable IMAP in your Gmail account available under Gmail Settings – > Forwarding and POP/IMAP – > Enable IMAP. Next launch the SMS backup app on your phone, enter your Google account credentials and the will immediately copy all existing text messages to a new folder /label in your Gmail account.


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