我创建了一个Android应用程序,因为我想要号码的最后通话详细信息。
使用 CallLog.Calls.DURATION 我收到最后一个通话详情。
我的代码是 -
StringBuffer sb = new StringBuffer();
Uri contacts = CallLog.Calls.CONTENT_URI;
Cursor managedCursor = context.getContentResolver().query(contacts, null, null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
if(managedCursor.moveToFirst())
{
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
String callDayTime = new Date(Long.valueOf(callDate)).toString();
// long timestamp = convertDateToTimestamp(callDayTime);
String callDuration = managedCursor.getString(duration);
int calld=Integer.parseInt(callDuration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + calld);
sb.append("\n----------------------------------");
}
managedCursor.close();
System.out.println(sb);
在一台设备中,我使用managedCursor.moveToFirst()
获取最后一个通话详情,在另一台设备中,我使用managedCursor.moveToLast()
获取最后一个通话详情。
如何使用单一代码实现它?
答案 0 :(得分:0)
您可以向查询添加ORDER BY子句,以便在调用时按顺序对它们进行排序。
Cursor managedCursor = context.getContentResolver().query(contacts, null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
OR
Cursor managedCursor = context.getContentResolver().query(contacts, null, null, null, CallLog.Calls.DATE + " DESC");