Reading SMS

  • The query used for reading the text messages is as following-
  • getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder)

  • The first parameter is the uri for the action we are doing. Other parameters are for filtering the selection of the messages. For now, we will keep all of them to null. The only thing that is mandatory is the uri. The above statement returns a Cursor.
  • First of all, import the following packages-
  • import android.database.Cursor;


    import android.provider.ContactsContract.CommonDataKinds.Phone;

    import android.provider.ContactsContract.Contacts;

  • The URI for reading messages is "content://sms/". So use it in the query() and save the result of query in a cursor.
  • Cursor cur=getContentResolver().query(Uri.parse("content://sms/"),null, null, null, null);

  • Now the cursor has all information of all messages. Start a while loop with moving the cursor to the next position. The loop will be ended when there will be no item left in the cursor.
  • while(cur.moveToNext()){


  • For retrieving the information from the cursor, use getColumnIndex() method of the class Cursor. Cursor is a kind of table that has different contacts in different rows and their different information in different columns.
  • String body=cur.getString(cur.getColumnIndex("body"));

  • Above statement will retrieve the body of the sms at which the cur is pointing at that time and store it in a String variable body.
  • Similarly sender's phone number and date of receiving can be accessed respectively using-
  • String add=cur.getString(cur.getColumnIndex("address"));

    String date=cur.getString(cur.getColumnIndex("date"));

  • Thus the code becomes-
  • Cursor cur=getContentResolver().query(Uri.parse("content://sms/"),null, null, null, null);


    String body=cur.getString(cur.getColumnIndex("body"));

    String add=cur.getString(cur.getColumnIndex("address"));

    String date=cur.getString(cur.getColumnIndex("date"));


  • In this way different information can be retrieved. Now you can use this information as you want.
  • Give the following permission in AndroidManifest.xml-
  • <uses-permission android:name="android.permission.READ_SMS" />