Android Program to Show SMS in Your Phone

«
»
Here is source code of the Program to Show SMS in Your Phone in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Main Activity

package com.example.read_sms;
 
//insipired from www.stack overflow.com and www.vogella.com/android/ 
import java.util.ArrayList;
import java.util.List;
 
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
 
public class MainActivity extends ListActivity {
 
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        List<SmsFormat> List = new ArrayList<SmsFormat>();
 
        Uri uri = Uri.parse("content://sms/inbox");
        Cursor cur = getContentResolver().query(uri, null, null, null, null);
        startManagingCursor(cur);
 
        if (cur.moveToFirst()) {
            for (int i = 0; i < cur.getCount(); i++) {
                SmsFormat sms = new SmsFormat();
                if (cur.getColumnIndexOrThrow("body") > -1) {
                    sms.setBody(cur
                            .getString(cur.getColumnIndexOrThrow("body"))
                            .toString());
                }
                if (cur.getColumnIndexOrThrow("address") > -1) {
                    sms.setNumber(cur.getString(
                            cur.getColumnIndexOrThrow("address")).toString());
                }
 
                String name = null;
                name = Search(cur.getString(
                        cur.getColumnIndexOrThrow("address")).toString());
 
                if (name != " ") {
                    sms.setName(name);
                    List.add(sms);
                }
                cur.moveToNext();
            }
        }
        cur.close();
 
        setListAdapter(new ListAdapter(this, List));
 
    }
 
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        SmsFormat sms = (SmsFormat) getListAdapter().getItem(position);
        Log.i("MSG", sms.getBody());
 
    }
 
    public String Search(String number) {
        Uri uri = Uri.withAppendedPath(
                ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(number));
        String name = " ";
 
        ContentResolver contentResolver = getContentResolver();
        Cursor contactLookup = contentResolver.query(uri, new String[] {
                BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME },
                null, null, null);
 
        try {
            if (contactLookup != null && contactLookup.getCount() > 0) {
                contactLookup.moveToNext();
                name = contactLookup.getString(contactLookup
                        .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            }
        } finally {
            if (contactLookup != null) {
                contactLookup.close();
            }
        }
 
        return name;
    }
}

List Adapter

package com.example.read_sms;
 
import java.util.List;
 
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
 
public class ListAdapter extends ArrayAdapter<SmsFormat> {
 
    private final Context context;
    private final List<SmsFormat> smslist;
 
    public ListAdapter(Context context, List<SmsFormat> 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.textView1);
 
        senderNumber.setText(smslist.get(position).getName() + "\n"
                + smslist.get(position).getNumber() + "\n" + "\n"
                + smslist.get(position).getBody());
 
        return rowView;
    }
 
}

Sms Format

advertisement
advertisement
package com.example.read_sms;
 
public class SmsFormat {
 
    private String number;
    private String body;
    private String name;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    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;
    }
 
}

Activity_Main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
 
</RelativeLayout>

Screenshot_2013-11-16-20-24-19

Participate in Java Programming Certification Contest of the Month Now!

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.