Android Receive SMS Intent: Get Message Id or Thread Id
Asked Answered
M

1

7

I have register a Broadcast listener to receive the android.provider.Telephony.SMS_DELIVER Intent action.

I seem to be able to get the body, and sender phone number of this new message.

However I am not able to get the Message Id or Thread Id of this new message. The only way I have found is to search the message and conversation tables for matching content. But this seems a little messy and I feel like there should be a neater way.

Side note:
Is there some SMS documentation that I have missed? Because a lot of the SMS api seems very undocumented.

Maffick answered 11/6, 2015 at 10:53 Comment(0)
M
4

There are no message IDs or thread IDs in SMS. Each SMS is a data packet that is completely independent of all other SMS. In Android, there is a standard SMS application that stores SMS in a database and "threads" conversations using some criteria which is not contained within the SMS message itself. However, third parties can also build SMS applications that manage the messages in a completely different way. This is very much dependent on the concept and implementation of the specific application that is managing the SMS. This is also why there isn't much documentation about the SMS system. The API that is available in Android is very low-level and very basic. Other applications build on top of this.

Maris answered 11/6, 2015 at 16:0 Comment(6)
Is there anyway to use this base Android application/service to determine what thread or the id of the message is in its maintained table? Short of trying to query the tables for matching message body and participants.Maffick
None that are reliable. For example, you could query the content provider (the SMS database is available as a content provider) and ask for the highest numbered ID in the database and assume that this is the most recent incoming message. This is, however, unreliable: There may be many incoming messages received in rapid succession; Also it takes some time for the incoming message to be stored and available, if you query to early you won't see the most recent message.Maris
So I tried the SQL query idea but you where right(unsurprisingly), when you receive the intent the Android tables have not been updated yet. Is there any way to listen for a change in the tables? Or am I sort of out of luck?Maffick
We have done that in the past. You can wait a certain amount of time (maybe 500 milliseconds or so) after you get the incoming message Intent, and then check the SMS database. You may need to play with the delay to get it right, but that should get you closer to your goal. Since you are in a BroadcastReceiver, you can't actually wait there. You need to start another Thread or post a Runnable to a Handler to do this.Maris
Just curious, how did you guys end up handling the new message? My current working idea is to just show a notification with the data from the intent, and the notification will point the user to the activity with a certain flag, and hopefully by this time the application will be able to query the database and figure out the message specifics.Maffick
Our solution was quite complicated but we were dealing with SMS that were intended for an application, not for a human. I can't really give you any help on that.Maris

© 2022 - 2024 — McMap. All rights reserved.