5

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

Marc Ortiz
  • 2,242
  • 5
  • 27
  • 46

1 Answers1

1

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);
PVS
  • 1,168
  • 11
  • 24
  • Hi @PVS, where do you get the value for lastOutgoingSmsTime? – midhunhk Jul 28 '15 at 03:39
  • lastOutgoingSmsTime is an app variable, that defines what "new" means to the app. Everytime you extract a set of SMS, just updated this timestamp for the next query. You'll have to handle it somehow the very first time. – PVS Jul 30 '15 at 11:59
  • got it. I stored the messageId to prefernces when ever a message is sent. then i compare the messageIds to check if its new. – midhunhk Jul 31 '15 at 18:01