Trying to read SMS/MMS on Android and geting java.lang.NullPointerException
Asked Answered
P

2

1

I'm trying to read SMS/MMS on Android, and I have followed the answer, when writing the code and try to run it on an Android OS 6.0.1 on Samsung device I got the following exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                      at android.os.Parcel.readException(Parcel.java:1626)
                      at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                      at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                      at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                      at android.content.ContentResolver.query(ContentResolver.java:502)
                      at android.content.ContentResolver.query(ContentResolver.java:445)
                      at com.my.code.services.ListenSmsMmsService$SMSObserver.onChange(ListenSmsMmsService.java:102)

This is the code that is creating the exception:

        public void onChange(boolean selfChange) {
            super.onChange(selfChange);


            /*first of all we need to decide message is Text or MMS type.*/
            final String[] projection = new String[] {"*"};

            Uri mainUri = Telephony.MmsSms.CONTENT_CONVERSATIONS_URI; //URI for query


            Cursor mainCursor = contentResolver.query(mainUri, projection, null, null, null);

The last line is the one that causes the crash. even if I used:

Uri mainUri = Uri.parse("content://mms-sms/conversations/");

and:

final String[] projection = new String[]{"_id", "ct_t"};

or:

final String[] projection = new String[]{Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN};

the crash happen.

When I tried to run a query on ContactsContract.PhoneLookup.CONTENT_FILTER_URI the query was successfull.

What can be the problem, that causes the crash?

Pelkey answered 14/12, 2016 at 23:56 Comment(10)
contentResolver.query(mainUri, project, null, null, null); project or projection ?Ionogen
if you change with this uri: content://mms-sms/conversations?simple=trueIgnoble
CONTENT_CONVERSATIONS_URI is meant to be used with a thread_id to return all of the messages in a single conversation. Apparently on some devices, you can query without one, and just get the list that content://mms-sms/conversations?simple=true would return, which should be the list of separate conversations, but I know (at least some) Samsungs won't have it. What are you trying to get, exactly? A single conversation? The list of conversations? Every message ever, no matter the conversation it belongs to?Aerodonetics
Thank you @toto, using your uri prevented the crash. But the projection didn't work.Pelkey
@Mike-M what I try to do is to get notified on any TextMessage (either SMS or MMS) that is received by the device. I also want to differentiate between SMS and MMS, and I have used the code sample in the answer I have quoted and it is not working, is there a way to do this differentiate? Is this: 'content://mms-sms/conversations?simple=true' URI is only working on Samsung devices and if the code will run on other device it will not work?Pelkey
TRY IT: String[] projection = {"Conversations._ID", "Conversations.ADDRESS", "Conversations.BODY"};Ignoble
This projection will not allow me to know if this message is SMS or MMS. How I can achieve this?Pelkey
"what I try to do is to get notified on any TextMessage..." - Querying the Provider is not going to give you a real-time notification of messages. It just returns the messages already saved when you do the query. "Is this ... URI is only working on Samsung..." - That URI should work everywhere that uses the standard API; i.e., any device where the OEM hasn't altered it. However, as mentioned, that's just going to return a list of conversation summaries, not the individual messages in them. If you want a list of every message, have a look at this post.Aerodonetics
Thanks, do you have an example of getting real time notification of mesages? BTW in my question I wrote that using what you wrote in the answer you pointed me to: 'final String[] projection = new String[]{Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN};' also makes the code crash.Pelkey
There are lots of examples on-site for receiving SMS. This post has the basics, though the message handling is slightly wrong, and a little outdated for API 19+. MMS is similar, but it's probable that your app won't get MMS with a Receiver on API 19+ if it's not the default messaging app, in which case a ContentObserver can be used. This post goes pretty in-depth on Receivers for both. As for my answer I linked above, look closely. It uses a different Uri than what you have; it's not CONTENT_CONVERSATIONS_URI.Aerodonetics
R
1

This happens on many other Samsung devices: seems that on these devices, you can't query for content://mms-sms/conversations?simple=true without the ?simple=true suffix - and when you add this suffix, it affects the returned columns, and that's why the projection failed.

See here for more related info about this, though no one really knows why it behaves this way :(

There might be a workaround, using an undocumented URI of content://mms-sms/complete-conversations - as you can read here.

Ricardaricardama answered 4/7, 2018 at 12:47 Comment(0)
A
0

I had the same problem with Samsung devices, so for just SMS conversations, I tried doing grouping by thread id on the SMS table here.

Unfortunately content://mms-sms/complete-conversations doesn't seem to aggregate on the thread id like content://mms-sms/conversations does, so you end up with multiple messages from the same thread.

Alleneallentown answered 17/6, 2020 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.