Listen to incoming/outgoing SMS

1, Listen to incoming SMS
1.1, Prepare manifest file
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
        package="org.apache.sms"> 
        <application>  
            <receiver class="SMSApp">  
                <intent-filter>  
                    <action android:value="android.provider.Telephony.SMS_RECEIVED" />  
                </intent-filter>  
            </receiver>  
       </application>  
       <uses-permission android:name="android.permission.RECEIVE_SMS" />
   </manifest>  

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 < 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 < 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
    <uses-permssion android:name="android.permission.READ_SMS" />
2) Delete/Modify/Send SMS
    <uses-permssion android:name="android.permission.WRITE_SMS" />
in AndroidManifest.xml
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值