Hoang
Hoang

Reputation: 857

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

Upvotes: 0

Views: 223

Answers (2)

Barmaley
Barmaley

Reputation: 16363

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

Upvotes: 1

SGX
SGX

Reputation: 3353

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

Upvotes: 0

Related Questions