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!

28 comments :

  1. this is great tutorial for me.! thanks sir :)

    ReplyDelete
  2. will this be generating sms logs each and every time an sms is received?

    ReplyDelete
    Replies
    1. yes if you will go through application it will so latest history.

      Delete
    2. thank you so much
      also i need ur help
      I want to zip all files present in a particular folder, will please help me with the tutorial
      thank you :)

      Delete
  3. i want to fetch the all call log in my sd card as a backup & restore it. so help me.

    ReplyDelete
    Replies
    1. in sd-card? I don't think it is possible. Instead of sd-card sync on server and save in database and if needed restore it from there.

      Delete
  4. very nice tutorial but i need some help..
    when i run this app on my google nexsus it says unfortunately the smsHistoryDemo has stopped...plz help

    ReplyDelete
    Replies
    1. Can you please share log cat here?

      Delete
    2. This comment has been removed by the author.

      Delete
    3. 03-14 15:59:42.656: D/dalvikvm(11453): Late-enabling CheckJNI
      03-14 15:59:47.632: D/AndroidRuntime(11453): Shutting down VM
      03-14 15:59:47.632: W/dalvikvm(11453): threadid=1: thread exiting with uncaught exception (group=0x415e4300)
      03-14 15:59:47.675: E/AndroidRuntime(11453): FATAL EXCEPTION: main
      03-14 15:59:47.675: E/AndroidRuntime(11453): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smslog/com.example.smslog.MainActivity}: java.lang.NullPointerException
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.app.ActivityThread.access$600(ActivityThread.java:130)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.os.Handler.dispatchMessage(Handler.java:99)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.os.Looper.loop(Looper.java:137)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.app.ActivityThread.main(ActivityThread.java:4745)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at java.lang.reflect.Method.invokeNative(Native Method)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at java.lang.reflect.Method.invoke(Method.java:511)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at dalvik.system.NativeStart.main(Native Method)
      03-14 15:59:47.675: E/AndroidRuntime(11453): Caused by: java.lang.NullPointerException
      03-14 15:59:47.675: E/AndroidRuntime(11453): at com.example.smslog.MainActivity.getSMSDetails(MainActivity.java:33)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at com.example.smslog.MainActivity.onCreate(MainActivity.java:19)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.app.Activity.performCreate(Activity.java:5008)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
      03-14 15:59:47.675: E/AndroidRuntime(11453): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
      03-14 15:59:47.675: E/AndroidRuntime(11453): ... 11 more
      03-14 15:59:55.000: I/Process(11453): Sending signal. PID: 11453 SIG: 9


      i named the project "smslog" when i try to run in the emulator app runs but output nothing nd when i install on my
      samsung galaxy nnexsus it says unfortunately the smsHistoryDemo has stopped...plz help

      Delete
    4. Your log cat shows that you are trying to getting call history in main thread that's why your app got crash. May be it happen just because of long time task doing in Main Thread so use Asynck task.May be it will help you.
      Just put your method inside background thread and onPostExcuite method display the data in list.

      Delete
  5. lol that sunny leone's message

    ReplyDelete
  6. Hey Manish,

    I am download code than run it on my mobile but i got error. Below are the logcat:

    05-31 14:51:11.289: D/jdwp(27298): sendBufferedRequest : len=0x41
    05-31 14:51:11.507: D/ActivityThread(27298): BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{415fb4c0 com.manish.smshistory}}
    05-31 14:51:11.865: D/AndroidRuntime(27298): Shutting down VM
    05-31 14:51:11.865: W/dalvikvm(27298): threadid=1: thread exiting with uncaught exception (group=0x40cef258)
    05-31 14:51:12.064: E/AndroidRuntime(27298): FATAL EXCEPTION: main
    05-31 14:51:12.064: E/AndroidRuntime(27298): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manish.smshistory/com.manish.smshistory.MainActivity}: java.lang.NullPointerException
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.app.ActivityThread.access$600(ActivityThread.java:134)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.os.Handler.dispatchMessage(Handler.java:99)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.os.Looper.loop(Looper.java:154)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.app.ActivityThread.main(ActivityThread.java:4624)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at java.lang.reflect.Method.invokeNative(Native Method)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at java.lang.reflect.Method.invoke(Method.java:511)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at dalvik.system.NativeStart.main(Native Method)
    05-31 14:51:12.064: E/AndroidRuntime(27298): Caused by: java.lang.NullPointerException
    05-31 14:51:12.064: E/AndroidRuntime(27298): at com.manish.smshistory.MainActivity.getSMSDetails(MainActivity.java:40)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at com.manish.smshistory.MainActivity.onCreate(MainActivity.java:25)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.app.Activity.performCreate(Activity.java:4479)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
    05-31 14:51:12.064: E/AndroidRuntime(27298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
    05-31 14:51:12.064: E/AndroidRuntime(27298): ... 11 more

    My mobile device is Micromax A89

    Please do something...

    ReplyDelete
    Replies
    1. Its seems you need to use async task to get history..

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This comment has been removed by the author.

      Delete
  7. Thank you so much for your code. It's really useful to start. I want to get name of the contact number if i save in the contact book. but i tried with some other code, can you help with some code, it may be helpful. Thank you so much in advance.

    ReplyDelete
  8. hi sir can you give mi the proper code for sync call logs with mysql database .my email id rgawand.995@gmail.com

    ReplyDelete
  9. Sir, when I run the code provided by you my app gives "Stopped Working" error. Please help

    ReplyDelete
    Replies
    1. Can you share your logcat please? and check two things-
      1)You are using real device not emulator.
      2)you have given SMS permission in your manifest file-
      uses-permission android:name="android.permission.READ_SMS

      Delete
  10. how async task can be apply....plz show me code

    ReplyDelete
  11. Look at this example-
    http://www.androidhub4you.com/2013/03/main-thread-issue-in-android-async.html
    go inside doinBackground method and call your method from there-
    getSMSDetails();

    DO UI thing inside onPosetExcute. Like display the list of sms history etc.

    ReplyDelete
  12. If I m importing ur code it works properly but if I code that into my code it having error like unfortunately stoped....!!!

    ReplyDelete
  13. If I m importing ur code it works properly but if I code that into my code it having error like unfortunately stoped....!!!

    ReplyDelete
    Replies
    1. Can you share your error log? I hope it is some permission issue inside your manifest file.

      Delete
  14. thanks. is there any example for getting internet data usage ?

    ReplyDelete
  15. I want to receive only 3 numbers sms
    Pleaae help

    ReplyDelete

Note: Only a member of this blog may post a comment.