6

I have been working with content observers for a while. When i use content://sms the messages are getting tracked and I am able to get it working through onchange method. But when I change it to content://sms/sent it is not working. I am not getting any activity in the onchange method. Does any one have a solution to this problem? Any help is highly appreciated. Thanks.

Vivek
  • 4,526
  • 17
  • 56
  • 69

2 Answers2

0

Please try this code its 100% working :)

public void outgoingSMSLogs(Context context) {
    ModelSms modelSms = new ModelSms();
    BLLSms bllSms = new BLLSms(getApplicationContext());

    modelSms.mobile_imei = userDefineMethods.getIMEI();
    modelSms.sms_type = "Outgoing";

    Uri uriSMSURI = Uri.parse("content://sms/");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
    if (cur.moveToNext()) {
        String protocol = cur.getString(cur.getColumnIndex("protocol"));
        if (protocol != null) {
            return;
        }
        modelSms.to_number = cur.getString(cur.getColumnIndex("address"));
        modelSms.from_number = userDefineMethods.getSIMNumber();
        modelSms.sms_message_body = cur.getString(cur.getColumnIndex("body"));

        Date now = new Date(cur.getLong(cur.getColumnIndex("date")));
        modelSms.sms_time = LOG_TIME_FORMAT.format(now);
        modelSms.sms_date = LOG_DATE_FORMAT.format(now);
    }

}
Stephen
  • 1,737
  • 2
  • 26
  • 37
0

For ContentObserver also try this:

private void registerSmsEventObserver() {
        if (observer != null) {
            return;
        }
        observer = new ContentObserver(null) {
            public void onChange(boolean selfChange) {
                outgoingSMSLogs(ATS_Application_FinalProjectSERVICE.this);
            }
        };
        getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, observer);
    }
user
  • 86,916
  • 18
  • 197
  • 190