Search code examples
javaandroidbroadcastreceivercalllog

New call log record not found in android broadcastreceiver


I am working on android application that needs phone calls to be logged on the server for personal use of course. I have broadcastreceiver registered for this purpose and I am able to detect all types of calls and their details. However, it is not possible to detect accurate outgoing call time i.e. we get to detect complete OFFHOOK time for outdoing call which is not exactly call duration talked. So only for outgoing call ended case I try to read records from the call log history. I am able to read call log history from broadcastreceiver for outgoing call ended but I don't see the entry of currently received call in the call log entry. I can see all the previous calls in the call history but not the one for which I received broadcastreceiver. I am using Gabe Sechan example from here in my code. And I try to read latest call history in outgoingcallended method like this,

    ContentResolver crs = globalContext.getContentResolver();
    Cursor cr = getAllCallLogs(crs);
    if(cr.moveToFirst()){
       duration = cr.getString(cr
                .getColumnIndex(android.provider.CallLog.Calls.DURATION));
        callNumber = cr.getString(cr
                .getColumnIndex(android.provider.CallLog.Calls.NUMBER));
       

    }
    private Cursor getAllCallLogs(ContentResolver cr) {
    // reading all data in descending order according to DATE
    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
    Uri callUri = Uri.parse("content://call_log/calls");
    Cursor cur = cr.query(callUri, null, null, null, strOrder);

    return cur;
    }

But the callNumber is previous call number and not the one for which I received broadcastreciver and same is the case with the duration. I understand that by the time I try to read call log it is not updated so how do I solve this? What am I missing?


Solution

  • You have to add some delay before getContentResolver(), so that table get updated. You can add either Thread.sleep() or can use handler.

    public void loadCursorPostDelayed(){
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
    
                ContentResolver crs = globalContext.getContentResolver();
                ...
            }
        }, 1500);
    }
    

    }