Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

android - Practical way to find out if SMS has been sent

I am interested in how I can figure out if SMS has been sent from the device.

In order to get notification when SMS is recieved, we use a broadcaster with:

android.provider.Telephony.SMS_RECEIVED

Important to mention that I do not send SMS from my app, I just should listen when SMS is sent from the device.

May be I should listen to some Content provider (which somehow related with SMS) and react for that change. Any ideas how I can achieve that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, It is possible to listen SMS ContentProvider by using ContentObserver

Here is my example for Outgoing SMS:

First register a ContetObserver with content://sms/

   public class Smssendservice extends Service{

       @Override  
       public void onCreate() {
            SmsContent content = new SmsContent(new Handler());  
            // REGISTER ContetObserver 
            this.getContentResolver().
                registerContentObserver(Uri.parse("content://sms/"), true, SMSObserver);  
       } 

       @Override
       public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub

            return null;
       }

SMSObserver.class

       public class SMSObserver extends ContentObserver {
            private Handler m_handler = null;

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

            @Override
            public void onChange(boolean selfChange) {
            super.onChange(bSelfChange);
            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) {
         //the message is sent out just now     
            }               
            else {
                 //the message is received just now   
            }
      }
  }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...