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.











1 comment:

Unknown said...

how i can get new send messages using

here this is my code im getting incoming messages but is not working for outgoing












public class incomingSms extends BroadcastReceiver
{
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

// Retrieves a map of extended data from the intent.
Bundle bundle = intent.getExtras();

if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
try {

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);

String message = currentMessage.getDisplayMessageBody();
String phoneNumber = currentMessage.getDisplayOriginatingAddress();

Date date = new Date(currentMessage.getTimestampMillis());
String formattedDate = new SimpleDateFormat("MM/dd/yyyy").format(date);

long time = date.getTime();
String formattedTime = new SimpleDateFormat("hh:mm:ss").format(time);

Toast.makeText(context, "senderNum: " + phoneNumber + ", message: " + message + " , date:" + formattedDate + ",time: " + formattedTime, Toast.LENGTH_LONG).show();


MyDBHandler db = new MyDBHandler(context);

int f_id = 1;
db.insertDat2(new DataSetGet(f_id, phoneNumber, message, formattedDate, formattedTime));
// addData(phoneNumber,message,formattedDate,formattedTime);

} // end for loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);

}
}
else if (intent.getAction().equals("android.provider.Telephony.SMS_SENT"))
{

if (bundle != null) {

Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) {

SmsMessage msgs = SmsMessage.createFromPdu((byte[]) pdus[i]);

String message =msgs.getDisplayMessageBody();
String phoneNumber = msgs.getDisplayOriginatingAddress();

Toast.makeText(context, "sending msg" + " number " + phoneNumber + "message" + message, Toast.LENGTH_LONG).show();

}

}
}
}
}