Monday, October 14, 2013

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

3 comments:

Unknown said...

How to convert date into dd/mm/yyyy format from miliseconds ?

Anonymous said...

by using simpleDateFormate Method

Anonymous said...

HI,

I have use this code, complete displaying the list of calllog
but
I one Question Is :

[1] not working the code in Above 8.0 Device

Please help me


if any permision required ?