Search code examples
javaandroidsmsmanager

Do I need to write into SMS Provider when set as default SMS app on Android?


I read this blog and this doc about default SMS app behavior on Android KitKat+. Both say that:

...if and only if an app is not selected as the default SMS app, the system automatically writes messages sent using this method to the SMS Provider (the default SMS app is always responsible for writing its sent messages to the SMS Provider).

So lets say from one place inside my code I need to send SMS. I use this code:

SmsManager smsManager = SmsManager.getDefault();

        smsManager.sendMultipartTextMessage(
                phoneNumber,
                null,
                smsManager.divideMessage(data),
                null, null);

Now when not set as default app, I can confirm that SMS appears in the default app after sending it from my app. If I were the default app on device, how do I insert the SMS? Where do I call this insert, is there some method that needs to be overloaded?

And when SMS is received, in the receiver class, do I write the SMS into internal SMS database as well? Again, how?

Thanks very much for help


Solution

  • If I were the default app on device, how do I insert the SMS?

    You can do it the same way you'd add to any ContentProvider; by calling insert() on the ContentResolver with the Telephony.Sms.Sent.CONTENT_URI and a ContentValues object with the relevant data.

    Where do I call this insert, is there some method that needs to be overloaded?

    You don't need to overload anything. Simply make the appropriate insert() call upon receiving the result from the sent PendingIntents that would be passed as the fourth parameter in your example sendMultipartTextMessage() call. Depending on the result, you might need to insert the message with type STATUS_FAILED.

    And when SMS is received, in the receiver class, do I write the SMS into internal SMS database as well?

    I'm not sure what you mean by internal SMS database, but the default app is responsible for writing all incoming messages to the Provider, which you would do in the same way as described above, but with the Telephony.Sms.Inbox.CONTENT_URI. If your app has its own separate database, you can write to that as well, using the standard database API.