Android – Fetch Inbox SMS

By -
Problem: Fetch Inbox SMS programatically
Description:

In this tutorial, I just have fetched SMS from the Inbox and displayed inside the ListView. SMS is fetched inside the ArrayList and then using this arraylist, I have created ArrayAdapter. Now just set this adapter to the ListView, that’s it.

Now, We can fetch the SMS from Inbox by making query to SMS content resolver by using:

Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);</pre>

Note: Don’t forget to add READ_SMS permission inside the AndroidManifest.xml file:

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

SMSInboxActivity.java

package com.pareshmayani.smsinbox;

import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * @author Paresh N. Mayani
 * (w): http://www.technotalkative.com/
 */
public class SMSInboxActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView lViewSMS = (ListView) findViewById(R.id.listViewSMS);

        if(fetchInbox()!=null)
        {
        	ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, fetchInbox());
        	lViewSMS.setAdapter(adapter);
        }
    }

    public ArrayList fetchInbox()
    {
    	ArrayList sms = new ArrayList();

    	Uri uriSms = Uri.parse("content://sms/inbox");
    	Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);

    	cursor.moveToFirst();
    	while  (cursor.moveToNext())
    	{
    		   String address = cursor.getString(1);
    		   String body = cursor.getString(3);

    		   System.out.println("======&gt; Mobile number =&gt; "+address);
    		   System.out.println("=====&gt; SMS Text =&gt; "+body);

    		   sms.add("Address=&gt; "+address+"n SMS =&gt; "+body);
    	}
    	return sms;

    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView
        android:id="@+id/listViewSMS"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>
    
</LinearLayout>

Download this Example: Android – Fetch Inbox SMS.

Paresh Mayani

Paresh Mayani is a lead android developer from India. Currently, He is Sr. Software engineer at InfoStretch Solutions Pvt. Ltd. He has been developing android apps since very 1st version i.e. Android 1.5, he says he has seen days and nights of Android history. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...