The following code shows how to register and event and observe changes made to the sms folder for send sms.
Most of the people states that the code may not work in future versions. Currently it works pretty with 2.1 and 2.2 and 2.3
public class SmsObserver extends ContentObserver {
private Context mContext;
private String contactId = "", contactName = "";
private String smsBodyStr = "", phoneNoStr = "";
private long smsDatTime = System.currentTimeMillis();
static final Uri SMS_STATUS_URI = Uri.parse("content://sms");
public SmsObserver(Handler handler, Context ctx) {
super(handler);
mContext = ctx;
}
public boolean deliverSelfNotifications() {
return true;
}
public void onChange(boolean selfChange) {
try{
Log.e("Info","Notification on SMS observer");
Cursor sms_sent_cursor = mContext.getContentResolver().query(SMS_STATUS_URI, null, null, null, null);
if (sms_sent_cursor != null) {
if (sms_sent_cursor.moveToFirst()) {
String protocol = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol"));
Log.e("Info","protocol : " + protocol);
//for send protocol is null
if(protocol == null){
/*
String[] colNames = sms_sent_cursor.getColumnNames();
if(colNames != null){
for(int k=0; k<colNames.length; k++){
Log.e("Info","colNames["+k+"] : " + colNames[k]);
}
}
*/
int type = sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex("type"));
Log.e("Info","SMS Type : " + type);
// for actual state type=2
if(type == 2){
Log.e("Info","Id : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("_id")));
Log.e("Info","Thread Id : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("thread_id")));
Log.e("Info","Address : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")));
Log.e("Info","Person : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("person")));
Log.e("Info","Date : " + sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date")));
Log.e("Info","Read : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("read")));
Log.e("Info","Status : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("status")));
Log.e("Info","Type : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("type")));
Log.e("Info","Rep Path Present : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("reply_path_present")));
Log.e("Info","Subject : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("subject")));
Log.e("Info","Body : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")));
Log.e("Info","Err Code : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("error_code")));
smsBodyStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")).trim();
phoneNoStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")).trim();
smsDatTime = sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date"));
Log.e("Info","SMS Content : "+smsBodyStr);
Log.e("Info","SMS Phone No : "+phoneNoStr);
Log.e("Info","SMS Time : "+smsDatTime);
}
}
}
}
else
Log.e("Info","Send Cursor is Empty");
}
catch(Exception sggh){
Log.e("Error", "Error on onChange : "+sggh.toString());
}
super.onChange(selfChange);
}//fn onChange
}//End of class SmsObserver
To register the content observer the following code lines are essential
static final Uri SMS_STATUS_URI = Uri.parse("content://sms");
SmsObserver smsSentObserver = new SmsObserver(new Handler(), this);
this.getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver);
For code please download from
https://sites.google.com/site/katharnavas/android/SMSTracker.zip
Comments on: "Listening for outgoing sms or send sms in Android" (16)
thank you for sharing this useful code . will be back for your articles.keep it up.
i tried your code in my application its getting force closed can you send me your full source code on my mail-id!!! asheesharya.cs@gmail.com
thanks in advance
i need your source code urgently .can you send me full source code on my mail-id asheesharya.cs@gmail.com
thanks in advance
I have send you the source code. Let me know if any further queries.
Hi, can you please tell me how to register the ContentObserver class in our activity. or it would be great if you could send me the source code at mohitramani.mr@gmail.com
Hope I have given the code to register the ContentObserver also. Please check the second block of code.
hey karthanavas thanks for sending the code. but the main problem i face now if i try to send sms form emulator sometime it display “msg body and address” two time ,three time and so on in logcat . but it work great when i receive sms it display one tim in logcat. can you tell me why this happened why it display more than one time if i try to send sms from my device.
Please do check the protocol value and type value. And also check whether the observer is getting called again and again.
i already checked its type =2 and protocol == null!! ontent observer executes three times four times and so on…..
Can you send me full code at dibya.sust@gmail.com
i need your source code urgently .can you send me full source code on my mail ersin.iljazi@gmail.com
thanks in advance
Reblogged this on lava kafle kathmandu nepal.
hi.. thanks for your code..
i’m trying to modify it to get the length of the of the recipient’s mobile phone no. Below is the code but it’s not working. Can you give some idea. Please bear with me since i’m a beginner in this android field. Thank you
String a;
Integer len;
a = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex(“address”)).toString();
len=a.length();
Log.e(TAG, “Address Length: ” + len);
Hi..
You said ” Currently it works pretty with 2.1 and 2.2 and 2.3″..
How to make the code works with android 4.x?
Thank you.
Hello, thanks for your post, but I always get null on following code:
Log.i(Constants.TAG, “Person : “+sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex(“person”)));
What did I miss ?
i want to get the phone no by this code
else if(intent.getAction().equals(“android.provider.Telephony.SMS_SENT”))
{
//what to add here pls help me..
}