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?
CONTENT_CONVERSATIONS_URI
is meant to be used with athread_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 thatcontent://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? – AerodoneticsContentObserver
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 differentUri
than what you have; it's notCONTENT_CONVERSATIONS_URI
. – Aerodonetics