最后发送的短信/短信:如何通过 ContentObserver 检索?

发布于 2024-10-09 23:27:04 字数 1016 浏览 1 评论 0原文

我知道存在一些问题,我想我已经解决了所有这些问题。

我的运营商很友善,每月提供 50 条免费短信。现在我想让自己成为一个在后台运行的小计数器,并在达到该限制时通知我。 (是的,我知道已经有这样的应用程序了)。因此,我希望每当发送短信/测试消息时都通知服务。因此,最好的方法是使用 ContentObserver。我设置了一个 (2) 并像这样注册它 (1):

(1)
Uri URI = Uri.parse("content://sms/sent");
String[] columns = {"_id", "date"};
Cursor c = context.getContentResolver().query(URI, columns, "type=?", new String[]{"1"}, "_id DESC");
Observer observer = new Observer(context);
c.registerContentObserver(observer);

(2)
private class Observer extends ContentObserver {
    Context context;
    public Observer(Context context) {
        super(null);
        this.context = context;
    }
    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        // some code here
    }
}

经过无数次的尝试,我不得不承认我自己无法使 onChange() 仅在短信时关闭已发送。相反,它也会在收到短信时关闭。

那么有人可以帮我解决这个问题吗?当然,我可以将所有传出短信写入数据库并查询以查明当前短信是否已存在,但这似乎是一个不整洁的解决方案,我想采取不同的做法。

预先感谢,
斯特夫

I know that there are some questions around and I've been through them all, I guess.

My carrier is kind enough to offer 50 free SMS each month. Now I would like to make myself a little counter that runs in the background and notifies me whenever I reach that limit. (Yes, I am aware that there will already be apps for this). Therefore, I would like a service to be notified whenever an SMS/test msg is sent. So the way to go would be a ContentObserver. I set one up (2) and register it like that (1):

(1)
Uri URI = Uri.parse("content://sms/sent");
String[] columns = {"_id", "date"};
Cursor c = context.getContentResolver().query(URI, columns, "type=?", new String[]{"1"}, "_id DESC");
Observer observer = new Observer(context);
c.registerContentObserver(observer);

(2)
private class Observer extends ContentObserver {
    Context context;
    public Observer(Context context) {
        super(null);
        this.context = context;
    }
    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        // some code here
    }
}

After countless attempts of messing I have to admit to myself I am plain unable to make the onChange() only go off when an SMS is sent. Instead, it goes off upon incoming SMS as well.

So can anyone help me out on this? Sure, I could write all outgoing SMS to a DB and query to find out if the current SMS already exists but that does seem like an untidy solution and I'd like to do differently.

Thanks in advance,
steff

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文