Read SMS Messages in Android

Read SMS Messages in Android

Android allows to read SMS messages using Telephony Provider. It manages access to a central repository that stores the data related to phone operation, SMS and MMS messages. The Telephony Provider is a content provider component.

Application should have access to read SMS messages. So, we need to request the READ_SMS permission in the manifest file.

app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <uses-permission android:name="android.permission.READ_SMS" />

    <application>
        ...
    </application>

</manifest>

Using the method ContextCompat.checkSelfPermission() we check if required permission was granted. If not, then we request permission by calling the method ActivityCompat.requestPermissions().

A ContentResolver object resolves a URI to the Telephony Provider. The method ContentResolver.query() returns a Cursor. We iterate through the Cursor using the method moveToNext() to retrieve a list of phone number, text and message type (1 - Inbox, 2 - Sent).

app/src/main/java/com/example/app/MainActivity.kt

package com.example.app

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Telephony
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity()
{
    private val permission: String = Manifest.permission.READ_SMS
    private val requestCode: Int = 1

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (ContextCompat.checkSelfPermission(this, permission)
            != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this, arrayOf(permission), requestCode)
        } else {
            readSms()
        }
    }

    private fun readSms()
    {
        val numberCol = Telephony.TextBasedSmsColumns.ADDRESS
        val textCol = Telephony.TextBasedSmsColumns.BODY
        val typeCol = Telephony.TextBasedSmsColumns.TYPE // 1 - Inbox, 2 - Sent

        val projection = arrayOf(numberCol, textCol, typeCol)

        val cursor = contentResolver.query(
            Telephony.Sms.CONTENT_URI,
            projection, null, null, null
        )

        val numberColIdx = cursor!!.getColumnIndex(numberCol)
        val textColIdx = cursor.getColumnIndex(textCol)
        val typeColIdx = cursor.getColumnIndex(typeCol)

        while (cursor.moveToNext()) {
            val number = cursor.getString(numberColIdx)
            val text = cursor.getString(textColIdx)
            val type = cursor.getString(typeColIdx)

            Log.d("MY_APP", "$number $text $type")
        }

        cursor.close()
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.