1

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.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
rogcg
  • 10,451
  • 20
  • 91
  • 133

3 Answers3

2

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.

IncrediApp
  • 10,303
  • 2
  • 33
  • 24
1

You are keeping track of calls using shared perefernces this is not efficient. Instead of you have to take one static variable and then you have to set its value whenever the state of call changed I had put a below code for keep track of call states and fetch new call log data from internal database This code is working fine for me.

public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);

switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        // Toast.makeText(context, "CALL_STATE_IDLE", Toast.LENGTH_LONG).show();
        /**this condition will check that last state was not CALL_STATE_IDLE means that 
        * recently there was disconneted call
        */
        if(UDF.phoneState != TelephonyManager.CALL_STATE_IDLE) {
            //You can query for new call log
        } 
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
         //Toast.makeText(context, "CALL_STATE_OFFHOOK", Toast.LENGTH_LONG).show();
        break;
    case TelephonyManager.CALL_STATE_RINGING:
         //Toast.makeText(context, "CALL_STATE_RINGING", Toast.LENGTH_LONG).show();
        endCallIfBlocked(incomingNumber);
        break;

    default:
        break;
}
UDF.phoneState = state;
}
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • Why you think using SharedPreferences is unefficient? I use it just to set a flag if there is new call or not. I verify that, if the state of phone is HOOKOFF, I set the flag to true, and then when I hang off the phone the state is IDLE and I check the flag in SharedPreference, and if its true, I call the IntentService, because I know that a OFFHOOK followed by an IDLE is a call. – rogcg Oct 12 '11 at 05:16
  • IDEL states calls more than once so that it is inefficient if you will use preference for that. – Dharmendra Oct 12 '11 at 05:32
  • Well according to the documentation, the state IDLE, is called when "Device call state: No activity." http://developer.android.com/reference/android/telephony/TelephonyManager.html#CALL_STATE_IDLE – rogcg Oct 12 '11 at 14:40
  • I created one activity for getting calllog and I was so confused due to call of state_idle and I had debug and knew that state_idle calls many times if you want to test it just create toast in idle states and check it.May be I am wrong.Thanks – Dharmendra Oct 12 '11 at 16:47
0

i just fixed making postdelay upto 4000ms while retrieve from db in idle state.

handler.postdelay(new Runnable(run() {
    //write code here
}), 4000);

it works fine to me...

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150