ContentObserver used for SMS

2019-02-10 09:45发布

I'm trying to extract the sent SMS. I know there is no BroadcastReciver for this. So I've found that I can use ContentObserver to listen changes in the db.

How can I implement this? My objective is to get only the new sms sent and send it via POST on the DB

Thanks

2条回答
对你真心纯属浪费
2楼-- · 2019-02-10 10:14

yes it is possible to Listen for new SMS by Registering ContentObserver to content://sms/

see in this post my answer how we Register ContentObserver to content://sms/

Practical way to find out if SMS has been sent

and you can also download working example from here

Contentovserver

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-10 10:23

Here is a code fragment to do this. The key is to use a selection that looks only for "type = outgoing messages".

Also since the content DB can get triggered by any change, keep track (somehow) of what has already been processed.

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);
查看更多
登录 后发表回答