Last sent SMS/text message: how to retrieve via ContentObserver?
I know that there are some questions around and I've been through them all, I guess.
My carrier is kind enough to offer 50 free SMS each month. Now I would like to make myself a little counter that runs in the background and notifies me whenever I reach that limit. (Yes, I am aware that there will already be apps for this). Therefore, I would like a service to be notified whenever an SMS/test msg is sent. 开发者_开发技巧So the way to go would be a ContentObserver
. I set one up (2) and register it like that (1):
(1)
Uri URI = Uri.parse("content://sms/sent");
String[] columns = {"_id", "date"};
Cursor c = context.getContentResolver().query(URI, columns, "type=?", new String[]{"1"}, "_id DESC");
Observer observer = new Observer(context);
c.registerContentObserver(observer);
(2)
private class Observer extends ContentObserver {
Context context;
public Observer(Context context) {
super(null);
this.context = context;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// some code here
}
}
After countless attempts of messing I have to admit to myself I am plain unable to make the onChange()
only go off when an SMS is sent. Instead, it goes off upon incoming SMS as well.
So can anyone help me out on this? Sure, I could write all outgoing SMS to a DB and query to find out if the current SMS already exists but that does seem like an untidy solution and I'd like to do differently.
Thanks in advance,
steff
精彩评论