How to read SMS in Android
In this tutorial we are going to show you how to read SMS messages under Android. Here is a simple program that displays a list of SMS messages stored on the device.
When an list element is clicked a SMS message body text is being displayed as a Toast message.
Here is the application code.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itcuties.android.apps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itcuties.android.apps.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You need to add a permission that allows your application to read SMS database. You need to add the permission to your application.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000">
<TextView
android:id="@+id/smsNumberText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NUMBER_GOES_HERE">
</TextView>
</LinearLayout>
Our application displays SMS senders numbers in a list. This layout represents single ListView row layout.
SMSData.java
package com.itcuties.android.apps.data;
/**
* This class represents SMS.
*
* @author itcuties
*
*/
public class SMSData {
// Number from witch the sms was send
private String number;
// SMS text body
private String body;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
This class represents SMS data. Our application uses only SMS sender number and the message body text.
ListAdapter.java
package com.itcuties.android.apps;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.itcuties.android.apps.data.SMSData;
/**
* List adapter for storing SMS data
*
* @author itcuties
*
*/
public class ListAdapter extends ArrayAdapter<SMSData> {
// List context
private final Context context;
// List values
private final List<SMSData> smsList;
public ListAdapter(Context context, List<SMSData> smsList) {
super(context, R.layout.activity_main, smsList);
this.context = context;
this.smsList = smsList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
TextView senderNumber = (TextView) rowView.findViewById(R.id.smsNumberText);
senderNumber.setText(smsList.get(position).getNumber());
return rowView;
}
}
This is our list adapter implementation. This class is responsible for building list rows. In the getView method a single ListView’s row is being constructed.
MainActivity.java
package com.itcuties.android.apps;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.itcuties.android.apps.data.SMSData;
/**
* Main Activity. Displays a list of numbers.
*
* @author itcuties
*
*/
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<SMSData> smsList = new ArrayList<SMSData>();
Uri uri = Uri.parse("content://sms/inbox");
Cursor c= getContentResolver().query(uri, null, null ,null,null);
startManagingCursor(c);
// Read the sms data and store it in the list
if(c.moveToFirst()) {
for(int i=0; i < c.getCount(); i++) {
SMSData sms = new SMSData();
sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString());
sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString());
smsList.add(sms);
c.moveToNext();
}
}
c.close();
// Set smsList in the ListAdapter
setListAdapter(new ListAdapter(this, smsList));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
SMSData sms = (SMSData)getListAdapter().getItem(position);
Toast.makeText(getApplicationContext(), sms.getBody(), Toast.LENGTH_LONG).show();
}
}
This class constructs a list of the SMS senders numbers. It extends a ListActivity. In the onCreate method of this class SMS database is being read. Results are stored in the list adapter. The main code that lets Android read SMS messages is this fragment.
Android read SMS code
Uri uri = Uri.parse("content://sms/inbox");
Cursor c= getContentResolver().query(uri, null, null ,null,null);
startManagingCursor(c);
if(c.moveToFirst()) {
for(int i=0; i < c.getCount(); i++) {
// Do something here for example read the body
String body = c.getString(c.getColumnIndexOrThrow("body")).toString();
c.moveToNext();
}
}
c.close();
As you can see we are connecting to the content database, the SMS inbox and query it for the SMS messages.
![]()
Download this sample code here.
![]()
This code is available on our GitHub repository as well.


Leave a Reply
Want to join the discussion?Feel free to contribute!