【问题标题】:Android: content observer for content://sms/sent not workingAndroid:content://sms/sent 的内容观察器不起作用
【发布时间】:2011-05-16 09:52:24
【问题描述】:

我与内容观察者合作已有一段时间了。当我使用content://sms 时,消息会被跟踪,我可以通过 onchange 方法让它工作。但是当我将其更改为 content://sms/sent 时,它不起作用。我在 onchange 方法中没有任何活动。有没有人可以解决这个问题?非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: android contentobserver


    【解决方案1】:

    请尝试此代码 100% 工作 :)

    public void outgoingSMSLogs(Context context) {
        ModelSms modelSms = new ModelSms();
        BLLSms bllSms = new BLLSms(getApplicationContext());
    
        modelSms.mobile_imei = userDefineMethods.getIMEI();
        modelSms.sms_type = "Outgoing";
    
        Uri uriSMSURI = Uri.parse("content://sms/");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
        if (cur.moveToNext()) {
            String protocol = cur.getString(cur.getColumnIndex("protocol"));
            if (protocol != null) {
                return;
            }
            modelSms.to_number = cur.getString(cur.getColumnIndex("address"));
            modelSms.from_number = userDefineMethods.getSIMNumber();
            modelSms.sms_message_body = cur.getString(cur.getColumnIndex("body"));
    
            Date now = new Date(cur.getLong(cur.getColumnIndex("date")));
            modelSms.sms_time = LOG_TIME_FORMAT.format(now);
            modelSms.sms_date = LOG_DATE_FORMAT.format(now);
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      ContentObserver 也可以试试这个:

      private void registerSmsEventObserver() {
              if (observer != null) {
                  return;
              }
              observer = new ContentObserver(null) {
                  public void onChange(boolean selfChange) {
                      outgoingSMSLogs(ATS_Application_FinalProjectSERVICE.this);
                  }
              };
              getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, observer);
          }
      

      【讨论】:

        猜你喜欢
        • 2010-10-14
        • 2013-03-31
        • 2015-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-26
        • 2018-07-05
        • 1970-01-01
        相关资源
        最近更新 更多