Sunday, February 16, 2014

Android SMS History | Get Messages Record in Android | Read SMS Log in Android Phone

Hello Friends,

This is a very simple demo application for Read SMS log in Android phone. All codes are below copy paste and enjoy.

1)MainActivity.java

package com.manish.smshistory;

import java.util.Date;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
/**
 *
 * @author manish
 *
 */

public class MainActivity extends Activity {

       TextView textView;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              textView = (TextView) findViewById(R.id.textview);
              getSMSDetails();

       }

       private void getSMSDetails() {
              StringBuffer stringBuffer = new StringBuffer();
              stringBuffer.append("*********SMS History*************** :");
              Uri uri = Uri.parse("content://sms");
              Cursor cursor = getContentResolver().query(uri, null, null, null, null);

              if (cursor.moveToFirst()) {
                     for (int i = 0; i < cursor.getCount(); i++) {
                           String body = cursor.getString(cursor.getColumnIndexOrThrow("body"))
                                         .toString();
                           String number = cursor.getString(cursor.getColumnIndexOrThrow("address"))
                                         .toString();
                           String date = cursor.getString(cursor.getColumnIndexOrThrow("date"))
                                         .toString();
                           Date smsDayTime = new Date(Long.valueOf(date));
                           String type = cursor.getString(cursor.getColumnIndexOrThrow("type"))
                                         .toString();
                           String typeOfSMS = null;
                           switch (Integer.parseInt(type)) {
                           case 1:
                                  typeOfSMS = "INBOX";
                                  break;

                           case 2:
                                  typeOfSMS = "SENT";
                                  break;

                           case 3:
                                  typeOfSMS = "DRAFT";
                                  break;
                           }

                           stringBuffer.append("\nPhone Number:--- " + number + " \nMessage Type:--- "
                                         + typeOfSMS + " \nMessage Date:--- " + smsDayTime
                                         + " \nMessage Body:--- " + body);
                           stringBuffer.append("\n----------------------------------");
                           cursor.moveToNext();
                     }
                     textView.setText(stringBuffer);
              }
              cursor.close();
       }

}


2)activity_main.xml

<ScrollView 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_margin="10dp"
        android:layout_height="wrap_content" />

</ScrollView>

3)manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.smshistory"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
     <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.manish.smshistory.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>


Thanks!