0

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?

Community
  • 1
  • 1
  • The reason is that the same record is not yet updated in DB when you received the broadcast. For that as correctly pointed out by Praveen you can add a delay but I would prefer adding a Content Observer Instead – Ayaanp Sep 20 '16 at 05:40

1 Answers1

0

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);
}

}

Praveen
  • 697
  • 6
  • 21
  • Thanks @Praveen it works with handler delay of 3 sec. But is delay method reliable? – Vinay Joshi Sep 20 '16 at 06:51
  • Delayed method will work in most cases until device is very slow. However a more concrete implementation will be using content observer suggested by Ayaanp. You will get call back on the data change in DB. On change you can check data for the last call. Please check [this] (http://stackoverflow.com/questions/16582577/android-content-observer-onchange-method-check-if-last-call-is-missed-call-or) – Praveen Sep 20 '16 at 07:25