I'm trying to get the call I just made/received. For that I'm using BroadcastReceiver and IntentServices.
It happens like this:
In my BroadcastReceiver class, I check if the state is OFFHOOK, if yes, I set the flag in SharedPreferences, so When the state becomes IDLE, I check if this flag is still true
, if it is, I call the IntentService that goes in CallLog *ContentProvider* and get the lates data like this:
cursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, mCursorFields, null, null, android.provider.CallLog.Calls.DATE + " DESC");
cursor.moveToFirst();
cursor.getString(cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
...
Until now it happens perfectly, the only problem is:
"It doesn't get the latest entry. It gets the next entry, that is the call made before this."
So, If I made a call before to person 'X', in the CallLog is registered this call to person 'X'. And now, if I make a call to person 'Y', instead of it get the call to person 'Y', it get's the call to person 'X', that was the call I made before this.
I don't know what can be happening. It was working perfectly when using ContentObserver but I thought it would be more efficiently built with BroadcastReceiver.
Any idea?
Thanks in advance.
Just a comment about using ContentObserver:
The problem with using ContentObserver is that, If I change the name of a contact, it detects a change in the CallLog, because the users in the CallLog with the old name will be updated to the new name, and the change will occur and call the class. For that I need to make verifications, for check if it really changed, which I think annoying.
Try delaying the call a bit. I believe it takes "a while" until the call log is added. ContentObserver is the correct way of handling this as you will be notified when the change occurs. If you want to work this way, you will need to delay this in a few seconds after the phone is idle.