0

I am developing an app that notify the user when any SMS marked as read even if the app isn't running

I simply created a contentobserver and I registered it in a service

the problem is that the contentobserver runs if the new SMS inserted or deleted but when the SMS marked as read ( Update operation) it doesn't work

here is my service code

    public class Smssendservice extends Service {

    private static Timer timer = new Timer();
    private Context ctx;

    public IBinder onBind(Intent arg0)
    {
        return null;
    }

    public void onCreate()
    {
        super.onCreate();
        ctx = this;
        startService();
    }

    private void startService()
    {
        //timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
        Toast.makeText(getApplicationContext(), "Before Register", Toast.LENGTH_SHORT).show();
        final Uri SMS_STATUS_URI = Uri.parse("content://sms");
        SMSLogger sl= new SMSLogger();

        SMSObserver smsSentObserver = new SMSObserver(sl, ctx);
        getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver);
        Toast.makeText(getApplicationContext(), "After Register", Toast.LENGTH_SHORT).show();
    }
}

I am registering my content observer in the service

here is the content observer code

public class SMSObserver extends ContentObserver
{
    SMSLogger smsLogger;
    Context context;

    public SMSObserver(SMSLogger smsLogger, Context context) {

        super(new Handler());
        this.context=context;
        this.smsLogger = smsLogger;
    }

    @Override
    public void onChange(boolean selfChange) {

        super.onChange(selfChange);
        smsLogger.querySMS(context);
    }
} 

eventually here is the SMS logger that I show the TOAST if the SMS data changed

public class SMSLogger {
    protected void querySMS(Context context) {
        Uri uriSMS = Uri.parse("content://sms/");
        Cursor cur = context.getContentResolver().query(uriSMS, null, null, null, null);
       /* cur.moveToNext(); // this will make it point to the first record, which is the last SMS sent
        String body = cur.getString(cur.getColumnIndex("body")); //content of sms
        String add = cur.getString(cur.getColumnIndex("address")); //phone num
        String time = cur.getString(cur.getColumnIndex("date")); //date
        String protocol = cur.getString(cur.getColumnIndex("protocol")); //protocol*/

        Toast.makeText(context, "Data Changed CHECK SMS" , Toast.LENGTH_SHORT).show();

    /*logging action HERE...*/
    }
}

it showed this message "Data Changed CHECK SMS" if new SMS inserted or SMS deleted but in case of update the toast doesnt appear. any clue ?

John Donvan
  • 554
  • 8
  • 22
  • should "content://sms" notify such an update? where did you read it? – pskink May 17 '14 at 15:36
  • it listens to insert and delete why it doesnt listen to update then ? do know how to listen to the update action ?? should I use sms/inbox ? – John Donvan May 17 '14 at 15:48
  • see https://developer.android.com/reference/android/provider/Telephony.Sms.Inbox.html – pskink May 17 '14 at 16:05
  • 1
    I have a question, what if we delete multiple messages from a thread, will be able to get notification of particular messages deleted? I am working on the given problem, but not able to reach to conclusion. – Bhupendra May 04 '15 at 10:24

1 Answers1

1

In your update method, check if the number of entries updated is more than 0. If it is, do getContext().getContentResolver().notifyChange(uri, null); before you return the number of entries updated.