Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2091287
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: 嵌入式

2009-03-03 17:01:06

  1. Monitor SMS
    • Monitor incoming SMS
      • Prepare manifest file
                   package="org.apache.sms"> 
                 
                     
                         
                             
                       
         
                   
         
              
         
              
          
          

        Remark: action android.provider.Telephony.SMS_RECEIVED is undocumented.
      • 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();
                            }
                        }    
                    } 
                } 
            } 

        If you want to block sms and prevent it from being shown on status bar and Messaging client, just invoke abortBroadcast () in BroadcastReceiver.
      • Reference:
        1. ApiDemos > OS > SMS Messaging
      • xxx
    • Monitor outgoing SMS  (Notes: following code is not tested)
      • Register observer for outgoing SMS
        class SMSHandler extends Handler
        {
            public void handleMessage(Message msg)
            {
                //Handle message
            }
        }

        class SMSObserver extends ContentObserver
        {
            private Handler m_handler = null;

            public SMSObserver(Handler handler)
            {
                super(handler);
                m_handler = handler;
            }

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

                //Send message to Activity
                Message msg = m_handler.obtainMessage;
                msg.obj = "xxxxxxxxxx";
                msg.sendToTarget();

                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();        //Enumerate all messages to check if it is in cache, if not, the message is sent out just now   
                else
                        onSMSReceive();     //Enumerate all messages to check if it is in cache, if not, the message is received just now   


            }
        }

        //First, cache all messages' signature (can contain addr, id, date, and hash code of subject + body)
        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.)
        //Release cache
      • 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();     
      • xxx
    • xxx
  2. 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 all messages which have the same thread_id(i.e. to delete all messages which thread_id is 2, the uri string is "content://sms/conversations/2")
      String strUriAll = "content://sms";//you can delete single message by _id (i.e. to delete a message which _id is 20, the uri string is "content://sms/20")
    //SMS in content provider are grouped by thread_id, and each message has unique _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 id = c.getString(0);  //Get id;
            String pid = c.getString(1);  //Get thread id;
            String uri = "content://sms/conversations/" + pid;
            mContext.getContentResolver().delete(Uri.parse(uri), null, null);  //you can also delete via id to delete single sms     
        }
        catch (Exception e)
        {
        }


    REMEBER: must request following permission
    1) Read SMS
       
    2) Delete/Modify/Send SMS
       
    in AndroidManifest.xml
  3. Send SMS
    You can send SMS with class SmsManager + SmsMessage, please refer to the sample ApiDemos > OS > SMS Messaging
  4. Reference
    • http://mobiforge.com/developing/story/sms-messaging-android
  5. xxx


阅读(9047) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~