---
title: "Listen to incoming/outgoing SMS"
author: "chunpeng"
date: "2009-11-23T20:36:14Z"
tags: ["Android", "C", "C#", "C++", "WordPress"]
slug: "listen-to-incoming-outgoing-sms"
draft: false
lastmod: "2009-11-23T20:36:14Z"
id: 523917
url: "https://chunpeng.iteye.com/blog/523917"
---

1, Listen to incoming SMS  
1.1, Prepare manifest file  
   &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"    
        package="org.apache.sms"&gt;   
        &lt;application&gt;    
            &lt;receiver class="SMSApp"&gt;    
                &lt;intent-filter&gt;    
                    &lt;action android:value="android.provider.Telephony.SMS\_RECEIVED" /&gt;    
                &lt;/intent-filter&gt;    
            &lt;/receiver&gt;    
       &lt;/application&gt;    
       &lt;uses-permission android:name="android.permission.RECEIVE\_SMS" /&gt;  
   &lt;/manifest&gt;  

Remark: action android.provider.Telephony.SMS\_RECEIVED is undocumented.

1.2, Parse SMS

    package org.apache.sms;   
     
    import android.content.BroadcastReceiver;   
    import android.content.Context;   
    import android.content.Intent;   
    import android.os.Bundle;   
    import android.telephony.gsm.SmsMessage;   
   
    public class SMSApp extends BroadcastReceiver  
    {   
        private static final String LOG\_TAG = "SMSApp";   
      
       /* package \*/  
        static final String ACTION =   
               "android.provider.Telephony.SMS\_RECEIVED";   
      
        public void onReceive(Context context, Intent intent)  
        {   
            if (intent.getAction().equals(ACTION))  
            {   
                Bundle bundle = intent.getExtras();  
                if (bundle != null)  
                {  
                    Object\[] pdus = (Object\[]) bundle.get("pdus");  
                    SmsMessage\[] messages = new SmsMessage\[pdus.length];  
                    for (int i = 0; i &lt; pdus.length; i++)  
                    {  
                        messages\[i] = SmsMessage.createFromPdu((byte\[]) pdus\[i]);  
                    }

                    for (SmsMessage message : messages)  
                    {  
                        String strFrom = message.getDisplayOriginatingAddress();  
                        String strMsg = message.getDisplayMessageBody();  
                    }  
                }      
            }   
        }   
    } 

Reference:  
http://davanum.wordpress.com/2007/12/15/android-listen-for-incoming-sms-messages/

2 Listen to outgoing SMS  (Remark: following code is not tested)  
2.1 Register observer for outgoing SMS  
class SMSHandler extends Handler  
{  
    public void handleMessage(Message msg)  
    {  
        //Handle message  
    }  
}

class SMSObserver extends ContentObserver  
{  
    private Handle m\_handle = null;

    public SMSObserver(Handle handle)  
    {  
        super(handle);  
        m\_handle = handle;  
    }

    public void onChange(boolean bSelfChange)  
    {  
        super.onChange(bSelfChange);

        //Send message o Activity  
        Message msg = new Message();  
        msg.obj = "xxxxxxxxxx";  
        m\_handle.sendMessage(msg);

        Uri uriSMSURI = Uri.parse("content://sms");  
        Cursor cur = this.getContentResolver().query(uriSMSURI, null, null,  
                     null, null);  
        cur.moveToNext();  
        String protocol = cur.getString(cur.getColumnIndex("protocol"));  
        if(protocol == null)  
                onSMSSend();             
        else  
                onSMSReceive();        

    }  
}

ContentResolver contentResolver = getContentResolver();  
Handler handler = new SMSHandler();  
ContentObserver m\_SMSObserver = new SMSObserver(handler);  
contentResolver.registerContentObserver(Uri.parse("content://sms/"),  
true, m\_SMSObserver);  //Register to observe SMS in outbox,we can observe SMS in other location by changing Uri string, such as inbox, sent, draft, outbox, etc.)

2.2 Parse SMS  
Uri uriSMSURI = Uri.parse("content://sms");  
Cursor cur = this.getContentResolver().query(uriSMSURI, null, null,  
null, null);  
cur.moveToNext();  
String protocol = cur.getString(cur.getColumnIndex("protocol"));  
if(protocol == null)  
    onSMSSend();         
else  
    onSMSReceive();     

3 Read and Delete All SMS  
/*  
  //Available Uri string  
  String strUriInbox = "content://sms/inbox";//SMS\_INBOX:1  
  String strUriFailed = "content://sms/failed";//SMS\_FAILED:2  
  String strUriQueued = "content://sms/queued";//SMS\_QUEUED:3  
  String strUriSent = "content://sms/sent";//SMS\_SENT:4  
  String strUriDraft = "content://sms/draft";//SMS\_DRAFT:5  
  String strUriOutbox = "content://sms/outbox";//SMS\_OUTBOX:6  
  String strUriUndelivered = "content://sms/undelivered";//SMS\_UNDELIVERED  
  String strUriAll = "content://sms/all";//SMS\_ALL  
  String strUriConversations = "content://sms/conversations";//you can delete one conversation by thread\_id  
  String strUriAll = "content://sms"//you can delete one message by \_id  
\*/

String strUriInbox = "content://sms/inbox";  
Uri uriSms = Uri.parse(strUriInbox);  //If you want to access all SMS, just replace the uri string to "content://sms/"  
Cursor c = mContext.getContentResolver().query(uriSms, null, null, null, null);  
while (c.moveToNext())  
{  
    try  
    {  
        //Read the contents of the SMS;  
        for(int i; i &lt; c.getColumnCount(); i++)  
        {  
            String strColumnName = c.getColumnName(i);  
            String strColumnValue = c.getString(i);  
        }

        //Delete the SMS  
        String pid = c.getString(1);  //Get thread id;  
        String uri = "content://sms/conversations/" + pid;  
        mContext.getContentResolver().delete(Uri.parse(uri), null, null);         
    }  
    catch (Exception e)  
    {  
    }  
} 

REMEBER: must request following permission  
1\) Read SMS  
    &lt;uses-permssion android:name="android.permission.READ\_SMS" /&gt;  
2\) Delete/Modify/Send SMS  
    &lt;uses-permssion android:name="android.permission.WRITE\_SMS" /&gt;  
in AndroidManifest.xml