Search code examples
androidphone-state-listener

How to get the outgoing call duration in real time?


I have a activity which is brought in front whenever there are any outgoing call. I need the outgoing call duration in real time and show it in my activity textview. Is this possible to get the current duration if call is in offhook?


Solution

  • This can be done. You just have to Spy on Android Call Log Content Provider. To use a content provider, you just need to call the getContentResolver() method. Once you have a content resolver object, you can query it using SQL or any of the built in query functions. For example, in my Android App, I use a piece of code that looks like this:

        String[] strFields = {
            android.provider.CallLog.Calls.NUMBER, 
            android.provider.CallLog.Calls.TYPE,
            android.provider.CallLog.Calls.CACHED_NAME,
            android.provider.CallLog.Calls.CACHED_NUMBER_TYPE,
            android.provider.CallLog.Calls.DURATION 
            };
    String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
    
    Cursor mCallCursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI,
            strFields,
            null,
            null,
            strOrder
            );