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?

有帮助吗?

解决方案

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

其他提示

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

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top