Android:内容观察者的内容://短信/发送不起作用

时间:2011-05-16 09:52:24

标签: android contentobserver

我一直在与内容观察员合作。当我使用content://sms时,消息会被跟踪,我可以通过onchange方法获得它。但当我将其更改为content://sms/sent时,它无效。我没有在onchange方法中获得任何活动。有没有人能解决这个问题?任何帮助都非常感谢。感谢。

2 个答案:

答案 0 :(得分:0)

请尝试使用此代码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);
    }

}

答案 1 :(得分:0)

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