3

I am trying to list all sms and mms threads/conversations in Android using Kotlin. The list returned should include the phone numbers in the conversation, snippet of latest message, and the Date and Time sent. Ideally they should be sorted by most recent activity.

I can list all sms messages individually (not by thread/conversation) with the following working code: (based on https://stackoverflow.com/a/9494532)

        val cursor: Cursor? = contentResolver.query(Uri.parse("content://sms"), null, null, null, null)

        if (cursor!!.moveToFirst()) { // must check the result to prevent exception
            do {
                var msgData = ""
                for (idx in 0 until cursor.getColumnCount()) {
                msgData += " " + cursor.getColumnName(idx).toString() + ":" + cursor.getString(idx)
                }
                Log.d("MY_APP", msgData)
            } while (cursor.moveToNext())
        } else {
            // empty box, no SMS
        }
        cursor.close()

I can also list all SMS conversations by replacing val cursor: Cursor? = contentResolver.query(Uri.parse("content://sms"), null, null, null, null) with:

val cr: ContentResolver = contentResolver
val conCursor = cr.query(Telephony.Sms.Conversations.CONTENT_URI, null, null, null, null, null)

The problem is that it only returns a snippet, thread_id, and msg_count. It does not return the phone numbers in the conversation, and it also ignores mms. This is intended behavior of Telephony.Sms.Conversations, as per https://developer.android.com/reference/kotlin/android/provider/Telephony.Sms.Conversations

The solution it would seem, is to use Telephony.Threads.CONTENT_URI. According to the documentation (https://developer.android.com/reference/kotlin/android/provider/Telephony.Threads), it includes mms, and returns all the required information.

But, when I replace val cursor: Cursor? = contentResolver.query(Uri.parse("content://sms"), null, null, null, null) with

val cr: ContentResolver = contentResolver
val conCursor = cr.query(Telephony.Threads.CONTENT_URI, null, null, null, null, null)

it throws the following error:

****-**-** 18:48:12.014 5267-5267/com.example.sms E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.sms, PID: 5267
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sms/com.example.sms.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2861)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6626)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
     Caused by: 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:2019)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)
        at android.content.ContentResolver.query(ContentResolver.java:754)
        at android.content.ContentResolver.query(ContentResolver.java:704)
        at com.example.sms.MainActivity.onCreate(MainActivity.kt:83)
        at android.app.Activity.performCreate(Activity.java:7032)
        at android.app.Activity.performCreate(Activity.java:7023)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6626) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

MainActivity.kt:83 is val conCursor = cr.query(Telephony.Threads.CONTENT_URI, null, null, null, null, null)

How can I list sms/mms threads with their phone number in Kotlin?

user7391836
  • 118
  • 8

0 Answers0