Search code examples
androidandroid-contentproviderandroid-contacts

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

Solution

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