Monday, October 14, 2013

Outgoing SMS details

In my previous post you can find how to read incoming SMS content programmatically.
In this post I will give you information about how to read outgoing SMS content. It's same as incoming SMS data reading, just need use different URI to read outgoing SMS content.
To read outgoing SMS content first add user permission:
 <uses-permission android:name="android.permission.READ_SMS"/>

Then call below method :

public StringBuffer getOutgoingSMSContent() {

ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://sms/sent/");
StringBuffer messagedata = new StringBuffer();
int count = 0;

Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
messagedata.append("Outgoing message count: " + (count +1) + "\n");
for (int m = 0; m < cursor.getColumnCount(); m++) {
if (cursor.getColumnName(m).equalsIgnoreCase("address")
|| cursor.getColumnName(m).equalsIgnoreCase("date")
|| cursor.getColumnName(m).equalsIgnoreCase("body")
|| cursor.getColumnName(m).equalsIgnoreCase("type"))
{
messagedata.append(cursor.getColumnName(m) + "  : "
+ cursor.getString(m));
messagedata.append("\n");
}
}
messagedata.append("\n");
count++;
} while (cursor.moveToNext());
}
}
cursor.close();
cursor = null;
return messagedata;
}

This method will return list of all outgoing SMS content.

Result will look something like this :


Here 'address' is mobile number of receiver.
'date' is message sent time in milliseconds.
'type' is message type incoming/outgoing. 2 is outgoing message type.
'body' is message text.











Incoming SMS details

Using Android API's user can read incoming SMS message content programmatically.
To get SMS content first add below user permission in manifest file:
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>

Then call below method from activity class:

public StringBuffer getIncomingSMSContent() {

ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://sms/inbox/");
StringBuffer messagedata = new StringBuffer();
int count = 0;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
messagedata.append("Incoming message count: " + (count +1) + "\n");
for (int m = 0; m < cursor.getColumnCount(); m++) {
if (cursor.getColumnName(m).equalsIgnoreCase("address")
|| cursor.getColumnName(m).equalsIgnoreCase("date")
|| cursor.getColumnName(m).equalsIgnoreCase("body")
|| cursor.getColumnName(m).equalsIgnoreCase("type"))
{
messagedata.append(cursor.getColumnName(m) + "  : "
+ cursor.getString(m));
messagedata.append("\n");
}
}
messagedata.append("\n");
count++;
} while (cursor.moveToNext());
}
}
cursor.close();
cursor = null;
return messagedata;
}

This method will return list of all incoming message content from inbox.

Here is the result :


In above result screen, 
address : mobile number from which device received message
date : message received time in milliseconds
type : message type incoming/outgoing. Type 1 indicates as incoming message type
body : message text