Read SMS on Android with screen damaged (solution only for devs)

Read SMS on Android with screen damaged (solution only for devs)

I came across a very interesting problem, the screen of my old Android phone got damaged and I badly wanted to read SMS on my phone. That had an old SIM card with a bigger size 🙁 So what to do? As is clear from the title that the solution is for developers, what I did was I used Android Android development skills and using that I logged all SMS from the device onto the screen of my laptop and how did I do that?

I did that using Android Studio and Android Debugger Bridge (ADB). So basically the solution is only for the developers the common man cannot do it if you really want to do it, then go and hire an Android developer and he or she can do it for you probably.

So coming to the solution, what I did was it created an Android app in using Android studio and I created an Activity and inside that Activity, I called a method would read all the SMS of that phone.

Prerequisites

Before jumping deep into the solution you need to consider:

  1. You need to have USB Debugging and Developer options turned on the phone.
  2. And because I was programming using that phone even before the screen got damaged, so I got lucky that I already had turned on USB Debugging on that phone.
  3. If you are not a developer then it might be a problem for you. Go ahead and hire an Andriod developer. Game Over!!
  4. What is the Android version of the phone? You need to accommodate that in the build.gradle file. Suppose your Android API Level is 8, then minSdkVersion in your build.gradle should be less than or equal to this number.
  5. If your phone is old, then for dependencies, you need to make sure that there is no reference to the latest libraries like Android.

The Code

Step 1

Create a New Android App using Android Studio.

Step 2

Set minSdkVersion less than or equal to the corresponding SDK version of your phone. Remove all dependencies, especially the new ones like androidx which are not compatible with old appcompat libs.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"

    defaultConfig {
        applicationId "dev.appsyoda.read_sms_broken"
        minSdkVersion 8
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    testImplementation 'junit:junit:4.12'
}

Notice the dependencies section.

Step 3

Add the permission to read SMS in AndroidManifest.xml. Notice the uses-permission tag. Check my full file.

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

    <uses-permission android:name="android.permission.READ_SMS" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 4

Modify the default MainActivity. First, we need to update the XML layout file, so as to make sure it is not using any advance Views or ViewGroups which are not part of older Android SDK versions (e.g. ConstraintLayout, etc).

Contents don’t matter but you just need to make it compatible with the SDK version of your phone. I hope you get the point.

Check my activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />

</FrameLayout>

Step 5

Add the function to read SMS in your MainActivity.java and call that method in onCreate() method.

It simply reads the SMS Inbox Content Resolver with URI content://sms/inbox and logs the desired entries to Logcat using Log.x method.

Here is my full MainActivity.java

package dev.appsyoda.sms_reader_broken_screen;


import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    public static final String TAG = MainActivity.class.getSimpleName();

    private void readSMS(){
        Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
        int messageCount = 10,count=0;
        if (cursor.moveToFirst()) { // must check the result to prevent exception
            do {
                if(++count==messageCount)
                    break;
                String msgData = "";
                for(int idx=0;idx<cursor.getColumnCount();idx++)
                {
                    //read only sender and the body of SMS
                    if(idx==3 || idx==12)
                    msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
                }

                // use msgData
                Log.e(TAG,"sms: "+msgData);
            } while (cursor.moveToNext());
        } else {
            // empty box, no SMS
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        readSMS();
    }
}

Step 6

Connect your phone to your system using USB Debugging and run the App.

Here is the result.

read-android-sms-logcat.png

That’s it. I hope you find this solution helpful. Write your doubts in Twitter DM @MohitSehgl