Search code examples
androidsmsbroadcastreceivermessageandroid-broadcast

How to track sent messages (SMS) in android?


I am developing an Android application which has one of it's features as to detect any messages (SMS) sent by an app to any number. For this I want to keep track of sent messages. I want to check the message as and when it is sent. I know there is a Broadcast Receiver for SMS Recieved which will automatically call the function when a message is received. Is there any method to do that for SMS Sent? I just need a technique to do that.


Solution

  • Create a ContentObserver

    public class InboxContentObserver extends ContentObserver {
    
        ContentResolver cr;
        Context ctxt;
    
        public InboxContentObserver(Handler handler, Context ctxt) {
            super(handler);
            this.cr = ctxt.getContentResolver();
            this.ctxt = ctxt;
        }
    
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.i(TAG, "Starting sms sync.");
            new Thread(new Runnable() {
                @Override
                public void run() {
                   Cursor sms_sent_cursor = cr.query(Uri
                            .parse("content://sms"), null, "date > ?",
                    new String[]{LAST_SYNC_TIME}, "_id asc");
                    //....write your code here
    
                }
            }).start();
        }
    }
    

    Replace LAST_SYNC_TIME with the time.

    Register the ContentObserver

    new InboxContentObserver(new Handler(), getApplicationContext());
            getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, inboxContentObserver);