Question

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?

Was it helpful?

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

OTHER TIPS

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top