Hello Friends,
This is a very simple demo application for Reading SMS log from Android phone. All codes are below copy paste and enjoy:)
Thanks!
This is a very simple demo application for Reading SMS log from 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>
this is great tutorial for me.! thanks sir :)
ReplyDeleteYour welcome dear!
Deletewill this be generating sms logs each and every time an sms is received?
ReplyDeleteyes if you will go through application it will so latest history.
Deletethank you so much
Deletealso i need ur help
I want to zip all files present in a particular folder, will please help me with the tutorial
thank you :)
i want to fetch the all call log in my sd card as a backup & restore it. so help me.
ReplyDeletein 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.
Deletevery nice tutorial but i need some help..
ReplyDeletewhen i run this app on my google nexsus it says unfortunately the smsHistoryDemo has stopped...plz help
Can you please share log cat here?
DeleteThis comment has been removed by the author.
Delete03-14 15:59:42.656: D/dalvikvm(11453): Late-enabling CheckJNI
Delete03-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
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.
DeleteJust put your method inside background thread and onPostExcuite method display the data in list.
lol that sunny leone's message
ReplyDeleteHey Manish,
ReplyDeleteI 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...
Its seems you need to use async task to get history..
DeleteThis comment has been removed by the author.
DeleteThis comment has been removed by the author.
DeleteThank 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.
ReplyDeletehi sir can you give mi the proper code for sync call logs with mysql database .my email id rgawand.995@gmail.com
ReplyDeleteSir, when I run the code provided by you my app gives "Stopped Working" error. Please help
ReplyDeleteCan you share your logcat please? and check two things-
Delete1)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
how async task can be apply....plz show me code
ReplyDeleteLook at this example-
ReplyDeletehttp://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.
If I m importing ur code it works properly but if I code that into my code it having error like unfortunately stoped....!!!
ReplyDeleteIf I m importing ur code it works properly but if I code that into my code it having error like unfortunately stoped....!!!
ReplyDeleteCan you share your error log? I hope it is some permission issue inside your manifest file.
Deletethanks. is there any example for getting internet data usage ?
ReplyDeleteI want to receive only 3 numbers sms
ReplyDeletePleaae help