Android:content://sms/sent 的内容观察器不起作用

发布于 2024-11-07 15:15:33 字数 189 浏览 3 评论 0原文

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

I have been working with content observers for a while. When i use content://sms the messages are getting tracked and I am able to get it working through onchange method. But when I change it to content://sms/sent it is not working. I am not getting any activity in the onchange method. Does any one have a solution to this problem? Any help is highly appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2024-11-14 15:15:33

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

}

Please try this code its 100% working :)

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

}
疧_╮線 2024-11-14 15:15:33

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

For ContentObserver also try this:

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文