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" (30)
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.
Hi, did you get the answer about how to make the code work for Android 4.*? If you did please help me how it’s urgent. Thanks in advance, if possible send me via my email :sirmekago7@gmail.com
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 ?
In latest api Boot Receiver is off ….. so how can register our receiver after device boot. Thanks.
Hi, is there any way to find out about the deletion of a message?
Nice example, BTW.
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..
}
Hi, thanks for your code. i have executed your code but i didn’t get output. I think onChange method is not called. when will be content changed for onChange method called?
please send me your code with activity to my Email id :murali6060.android@gmail.com
Thanks.
i want to show sms log details in my app, it worked in android 4.0.4 version but not worked in android 2.3.6 version and i already took minSDK Version “8” and maxSDK Version “17”.
I am sending two messages. The first one is the phone closed. The second is the phone open. The first update is the latter. The second update is the first I sent the phone opens. With this ObServer I can access only the most recent post. Showing only the most recent data. How can I get the new updated data?
my onchange method is not called
so thank you,
i want this code !!!
plz send me full source code on my email id rajnishk556@gmail.com
Nice…. Good work…
Hello , If we want to track the sms send by third party applicatin which is using SMS manager class but not using local database then how can we track such application …
any help would be appriciated..
can you please send me the whole code to my mail id satya.ajay553@gmail.com
please i want full source code please send it to amanyelkhedry@gmail.com
thanks a lot!!!