如何获取通话记录的联系人图像和ID?

时间:2018-01-01 11:39:39

标签: android calllog

我试图阅读所有通话记录并以非常有条理的方式显示它们。我能够获得通话记录。但是,我需要知道我的电话簿中是否已存在来电者的联系。 CACHED_NAME并不总是返回值(即使将联系人保存在设备中也是如此)。

这就是我现在获取通话记录的方式:

private ArrayList<JSONObject> fetchCallLogs() {
    ArrayList<JSONObject> jsonArray = new ArrayList<>();

    try {
        Cursor cursor   = getContentResolver().query( CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC" );
        int name        = cursor.getColumnIndex( CallLog.Calls.CACHED_NAME );
        int number      = cursor.getColumnIndex( CallLog.Calls.NUMBER );
        int type        = cursor.getColumnIndex( CallLog.Calls.TYPE );
        int date        = cursor.getColumnIndex( CallLog.Calls.DATE );
        int duration    = cursor.getColumnIndex( CallLog.Calls.DURATION );

        while( cursor.moveToNext() ) {
            String callName     = cursor.getString( name );
            String callNumber   = cursor.getString( number );
            String callType     = cursor.getString( type );
            String callDuration = convertSecondsToHHMMSS( cursor.getString( duration ) );
            String callDateTime = DateFormat.format( "hh:mm:ss (dd MMM, yyyy)", new Date( Long.valueOf( cursor.getString( date ) ) ) ).toString();

            String dir          = "";

            switch( Integer.parseInt( callType ) ) {
                case CallLog.Calls.OUTGOING_TYPE:
                    dir = "Outgoing";
                    break;

                case CallLog.Calls.INCOMING_TYPE:
                    dir = "Incoming";
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    dir = "Missed";
                    break;
            }

            switch( Integer.parseInt( callFeature ) ) {
                case CallLog.Calls.FEATURES_VIDEO:
                    callFeaType = "Video";
                    break;
            }

            JSONObject callRecord = new JSONObject();

            try {
                callRecord.put( "name", callName );
                callRecord.put( "number", callNumber );
                callRecord.put( "type", dir );
                callRecord.put( "date", callDateTime );
                callRecord.put( "duration", callDuration );
            } catch (JSONException e) {
                e.printStackTrace();
            }

            jsonArray.add( callRecord );
        }
        cursor.close();
    } catch ( SecurityException e ) {
        e.printStackTrace();
    }

    return jsonArray;
}

我想获取联系人图片和CONTACT_ID日志(如果它们存在于设备中)以及上述结果。

我的最低API级别为21。

1 个答案:

答案 0 :(得分:0)

要获取通话记录详细信息,请致电CallLog.Calls.CONTENT_URI()

Cursor contact = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID + "=?", new String[]{String.valueOf(148)}, null, null);
    if (contact != null && contact.moveToNext()) {
        String lookupKey = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
        int contactId = contact.getInt(contact.getColumnIndex(ContactsContract.Contacts._ID));
        Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);

        Cursor calls = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.CACHED_LOOKUP_URI + "=?", new String[]{contactUri.toString()}, CallLog.Calls.DATE + " DESC");
        while (calls.moveToNext()) {
            Log.d(TAG, "type: " + calls.getString(calls.getColumnIndex(CallLog.Calls.TYPE)) + ", number: " + calls.getString(calls.getColumnIndex(CallLog.Calls.NUMBER)) + ", " + "cached_name: " + calls.getString(calls.getColumnIndex(CallLog.Calls.CACHED_NAME)) + ", " + "date: " + calls.getString(calls.getColumnIndex(CallLog.Calls.DATE)));
        }
    }

有关通话记录的详情,请阅读this

希望它能帮到你!!