这是获取最后通话时间的代码。试试吧,它有效。最后一次通话时长是从通话记录中获取的。
public void getCallLog() {
StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, 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 :");
Log.e("total count", "" + managedCursor.getCount());
//managedCursor.moveToPosition(managedCursor.getCount() - 1);
int currentCount = 0, lastPosition = 0;
while (managedCursor.moveToNext()) {
currentCount++;
//managedCursor.moveToPosition(managedCursor.getCount() - 1);
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
// lastPosition = currentCount;
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
lastPosition = currentCount;
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
Log.e("test", sb.toString());
}
lastPosition--;
managedCursor.moveToPosition(lastPosition);
int requiredNumber = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int durations = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
String phNumber = managedCursor.getString(requiredNumber);
String dur = managedCursor.getString(durations);
textView.setText(phNumber);
textDuration.setText(dur);
Log.e("last position number ", phNumber);
Log.e("last call Duration ", dur);
managedCursor.close();
}