I have setup a background service to registerContentObserver
to get notified whenever an SMS is sent. Upon receiving this event, I would increment a variable to know the count of messages sent. This is working as expected.
When someone sends SMS with more than 140 characters, the mobile carrier would treat this as multiple SMS, but it seems that I get only 1 callback for the sent message. This is causing my app to miss counting some messages.
Is there any proper way to know how many messages were actually sent?
When an app is responsible for writing its own messages to the Provider, it's most likely going to write the whole message in one go, regardless of whether the message must be sent as multipart. This would be why your Observer is often firing only once for each complete message, no matter how big.
Since KitKat, the system will automatically save the outgoing messages for any non-default apps, and for multipart messages, each part will be saved individually, firing your Observer each time. Of course, this doesn't help for anything prior to KitKat, or if a default app saves its own messages on later versions.
One possibility is to fetch the message body in your
ContentObserver
, and determine how many message parts it would've been split into. TheSmsMessage.calculateLength()
method can do this for us. It returns anint
array, the first element of which will have the message count for the given text.For example, using the old
onChange(boolean)
method, to support API < 16:Should you be supporting API 16 and above, we can use the
onChange(boolean, Uri)
overload, and things get a little simpler, since we don't necessarily need to keep track of the last message ID.