【发布时间】:2018-01-01 11:39:38
【问题描述】:
我正在尝试阅读所有通话记录并以非常有条理的方式显示它们。我可以得到通话记录。但是,我需要知道来电者的联系人是否已经存在于我的电话簿中。 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。
【问题讨论】:
-
看看this
-
@HemantParmar 谢谢,但该链接无济于事,因为我的要求是从通话记录中获取联系人详细信息,而此链接是关于直接获取联系人的。