实用的方法,以找出是否短信已发送短信、方法

2023-09-12 22:30:09 作者:穿着校服猖狂世界

我很感兴趣,我怎么能计算出,如果短信已发送的设备。

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?

推荐答案

是的,它可以通过使用听短信的ContentProvider ContentObserver

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

下面是我的例子中拨出短信:

Here is my example for Outgoing SMS:

首先注册内容ContetObserver://短信/

   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   
            }
      }
  }

}