【问题标题】:ContentObserver used for SMS [duplicate]用于 SMS 的 ContentObserver [重复]
【发布时间】:2012-07-02 03:37:06
【问题描述】:

我正在尝试提取发送的短信。我知道这没有 BroadcastReciver 。所以我发现我可以使用 ContentObserver 来监听 db 中的变化。

我该如何实现呢?我的目标是只发送新的短信并通过数据库上的 POST 发送

谢谢

【问题讨论】:

    标签: android sms contentobserver


    【解决方案1】:

    这是执行此操作的代码片段。关键是使用只查找“type = 外发消息”的选择。

    此外,由于任何更改都可能触发内容数据库,因此请跟踪(以某种方式)已处理的内容。

    int THREAD_ID = 0, ADDRESS = 1, DATE = 2, TYPE = 3, BODY = 4, INCOMING = 1, OUTGOING = 2, UNKNOWN = -1;
    
    String[] smsProjection = new String[] {"thread_id", "address", "date", "type", "body"};
    
    ContentResolver cr = context.getContentResolver();
    
    Cursor cursor = context.getContentResolver().query(uri, smsProjection, "type = ? AND date > ?",new String[]{Integer.toString(OUTGOING), Long.toString(lastOutgoingSmsTime)}, null);
    

    【讨论】:

    • 嗨@PVS,你从哪里获得 lastOutgoingSmsTime 的值?
    • lastOutgoingSmsTime 是一个应用变量,它定义了“新”对应用的意义。每次提取一组 SMS 时,只需更新此时间戳以供下一次查询。您必须在第一时间以某种方式处理它。
    • 知道了。发送消息时,我将 messageId 存储到首选项。然后我比较 messageIds 来检查它是否是新的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    相关资源
    最近更新 更多