How to access SMS or read last SMS in your phone using your App – Android Studio Java code?

This video shows the steps to design your custom App in Android Studio which can access the SMS data from your phone. In this video, first it shows the steps to design a simple App which uses just a Button and Text Box. Further, it shows how the permission to access the SMS can be sought by updating the Manifest file and respective Code in the Java file. Then it shows how the Read SMS method to read the data using the getcontentResolver method and finally it displays the read data on the text box using the setText method.

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

package com.example.mysmsapp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView myTextView;

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

myTextView = findViewById(R.id.textView);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS}, PackageManager.PERMISSION_GRANTED);
}

public void Read_SMS(View view){

Cursor cursor = getContentResolver().query(Uri.parse(“content://sms”), null, null,null,null);
cursor.moveToFirst();

myTextView.setText(cursor.getString(12));

}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.mysmsapp”>

<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>

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”150dp”
android:layout_marginTop=”50dp”
android:onClick=”Read_SMS”
android:text=”@string/readsms” />

<TextView
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”100dp”
android:layout_marginTop=”200dp”
android:text=”@string/textview” />
</RelativeLayout>

2 comments

  1. Dear Ranjit,

    That’s exactly what is shown in my below tutorial:
    https://programmerworld.co/android/how-to-control-audio-or-ringtone-settings-of-your-android-phone-remotely-without-internet-using-sms/

    Here, I have shown how to mute and unmute your phone. But of course you can use the same concept to set the ringtone and ring your phone or start the alarm through SMS keywords (commands).

    Let me know if it helps!

    Good Luck
    Programmer World
    https://www.youtube.com/c/programmerworld

Leave a Reply

%d bloggers like this: