3

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?

Kara
  • 6,115
  • 16
  • 50
  • 57
Deepak Jangir
  • 580
  • 5
  • 13

2 Answers2

3

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
        );
latsha
  • 1,298
  • 1
  • 14
  • 22
The Dark Knight
  • 5,455
  • 11
  • 54
  • 95
0
        if (callType == OUTGOING_CALL_END || callType == INCOMING_CALL_END) {
                        Cursor cursorCallLogs = getRecentCallLogs(context.getContentResolver());
                        if (cursorCallLogs != null) {
                            cursorCallLogs.moveToLast();
                            do {
                                try {
                                    String callNumber = cursorCallLogs.getString(cursorCallLogs.getColumnIndex("number"));
                                    callNumber = utilS.getPhoneNumber(callNumber);
                                    utilS.log("Phone NUmber", callNumber);
                                    duration = (cursorCallLogs.getInt(cursorCallLogs.getColumnIndex("duration")) * 1000);
                                    utilS.log("durationCall", "" + duration);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            while (cursorCallLogs.moveToPrevious());
                        }

                    }



    public static Cursor getRecentCallLogs(ContentResolver cr) {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
        return null;
    } else {
        return cr.query(CallLog.Calls.CONTENT_URI, null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 1");
    }

}
Brinda Rathod
  • 2,693
  • 1
  • 20
  • 32