3

每当有任何拨出电话时,我都会将其放在前面。我需要实时拨出通话时间并将其显示在我的活动文本视图中。如果通话处于摘机状态,这是否可以获得当前持续时间?

4

2 回答 2

3

这是可以做到的。你只需要监视Android Call Log Content Provider. 要使用内容提供者,您只需要调用该getContentResolver()方法。一旦有了内容解析器对象,就可以使用 SQL 或任何内置查询函数对其进行查询。例如,在我的 Android 应用程序中,我使用了如下所示的一段代码:

    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
        );
于 2013-06-07T11:42:57.877 回答
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");
    }

}
于 2019-01-07T12:11:16.507 回答