I know you are tempted to mark this a duplicate but wait, let's go through this again with my detailed (but failed) attempts.
Strategy 1: Algorithm: Answer
The first time the onChange is fired, get the id of the row updated
The next time the onChange is fired again get id of row updated
Match the id
Ignore if same id
Problem with this approach is that it is vulnerable to race conditions. If by the time you get the id of the updated row, onChange has fired for the second time, this Algorithm fails. This originated from my personal experience while testing on slow machines or machines working at peak capacity.
Strategy 2: Algorithm:Answer
Override deliverSelfNotifications() to return true.
This seems promising at first but fails to work. The code I used for reference:
In Main Activity:OnCreate Method I register:
getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, new CtObserver(new Handler()));
Then in a separate Class:
package com.example.testproject;
import android.database.ContentObserver;
import android.os.Handler;
import android.util.Log;
/**
* @author Time Traveller
*/
public class CtObserver extends ContentObserver {
public CtObserver(Handler handler) {
super(handler);
}
public boolean deliverSelfNotifications(){
return true;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.e("onChange","Fired");
}
}
Why you should contribute to this answer:
Quering SMS content Provider is the only way for Non Default Apps to capture the event for the Sent SMS. But till now I have not found any convincing fully functional answer for this method. So we really need to get some minds on this !!.
Questions:
You need not answer all the questions, just tell us anything you know.
deliverSelfNotifications()
most possibly designed to separate data changes from presentation changes. For example, content could be sorted in your app, but there are no need for external applications to acquire data again just because it was sorted, since most time this will be irrelevant (that application probably uses different presentation anyway). In other words - this method controls if you want to receive changes made to content by provider itself which may or may not actually reflect data changes in meaningful to you application ways.
In order to use it you need a ContentObservable
implementation that makes use of self-change notifications.
For another question i'd like to suggest HashSet
for storing message ids. With it you can compare id of message to all ever processed messages, not just last one, thus eliminating problem you stated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With