3

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

michnovka
  • 2,880
  • 3
  • 26
  • 58

1 Answers1

3

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.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Thanks for the answer, one more thing though. When another app (nondefault sms app) invokes the send as I described above, is my app notified and responsible for handling that as well? – michnovka Dec 05 '15 at 03:01
  • 1
    Nope. A non-default app using `SmsManager` to send has its messages written to the Provider by the system automatically. You don't have to deal with that, other than making sure you display them in your app, which would happen anyway if you query the Provider to create your message list. – Mike M. Dec 05 '15 at 03:10
  • As for mms, those are not put intosms provider in nondefault app, so there is no way to show them in def app when sent from nondefault? – michnovka Dec 05 '15 at 03:12
  • MMS is tricky and difficult to implement. Considering also that current messaging apps are expected to hand off MMS sends to the default app, it is very unlikely that you'll have an app on your device that will send MMS on its own. If such an app does do that, it won't have write access to the Provider, so your app really won't have any way of knowing it has done so. – Mike M. Dec 05 '15 at 03:17
  • Last question (thx for all the answers), is there any event fired when some other app sends sms using def SMSProvider? So I could setup listener and do sth when that happens. – michnovka Dec 05 '15 at 03:20
  • If you mean using `SmsManager`, your app can set a `ContentObserver` on the Provider that checks for changes to the `Sent` messages. Other than that, no, there is no broadcast when that happens, unfortunately. – Mike M. Dec 05 '15 at 03:23
  • 1
    hey @MikeM. please take a look at - http://stackoverflow.com/questions/37320172/write-received-message-to-sms-provider-kitkat-and-later – Sagar Nayak May 19 '16 at 10:23
  • @MikeM. Can you please take a look into that - https://stackoverflow.com/questions/65705690/is-there-a-way-that-i-can-clear-the-broadcast-of-sms-sent-or-sms-delivered-so-th – Aman Verma Jan 14 '21 at 19:37
  • I am not getting the SMS_SENT receiver and SMS_DELIVERED receiver in Oppo, vivo – Aman Verma Jan 14 '21 at 19:38