ContentObserver用于SMS

Mar*_*tiz 5 sms android contentobserver

我正在尝试提取已发送的短信.我知道没有BroadcastReciver.所以我发现我可以使用ContentObserver来监听数据库中的更改.

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

谢谢

PVS*_*PVS 1

这是执行此操作的代码片段。关键是使用仅查找“类型 = 传出消息”的选择。

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

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);
Run Code Online (Sandbox Code Playgroud)