A Guide to Mobile and Web Technology(LAMP)

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

About these ads

Comments on: "Listening for outgoing sms or send sms in Android" (27)

  1. thank you for sharing this useful code . will be back for your articles.keep it up.

  2. 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

  3. i need your source code urgently .can you send me full source code on my mail-id asheesharya.cs@gmail.com
    thanks in advance

  4. 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.

  5. i already checked its type =2 and protocol == null!! ontent observer executes three times four times and so on…..

  6. Can you send me full code at dibya.sust@gmail.com

  7. i need your source code urgently .can you send me full source code on my mail ersin.iljazi@gmail.com
    thanks in advance

  8. 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);

  9. 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.

  10. 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 ?

  11. Faisal Wahab said:

    In latest api Boot Receiver is off ….. so how can register our receiver after device boot. Thanks.

  12. Hi, is there any way to find out about the deletion of a message?
    Nice example, BTW.

  13. 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..
    }

  14. 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.

  15. 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”.

  16. Abdulkadir NURKALEM said:

    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?

  17. my onchange method is not called

  18. so thank you,
    i want this code !!!

  19. plz send me full source code on my email id rajnishk556@gmail.com

  20. Nice…. Good work…

  21. 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..

  22. satya gowri said:

    can you please send me the whole code to my mail id satya.ajay553@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 40 other followers

%d bloggers like this: