I have a code that works below. But I can't limit it. I just want to get the last 20 call logs. But that's how I see all-time search logs.
It should only be the last call logs and I only need to see 20 pieces. Any help, I'd appreciate it.
My Code;
private void getCallLogs() {
ContentResolver cr = getBaseContext().getContentResolver();
Cursor c = cr.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
int totalCall = 1;
if (c != null) {
totalCall = c.getCount();
if (c.moveToFirst()) {
for (int j = 0; j < totalCall; j++) {
String callDate = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DATE));
String phNumber = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
String callDuration = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DURATION));
Date dateFormat= new Date(Long.valueOf(callDate));
String callDayTimes = String.valueOf(dateFormat);
String direction = null;
switch (Integer.parseInt(c.getString(c.getColumnIndexOrThrow(CallLog.Calls.TYPE)))) {
case Telephony.Sms.MESSAGE_TYPE_INBOX:
direction = "OUTGOING";
break;
case Telephony.Sms.MESSAGE_TYPE_SENT:
direction = "INGOING";
break;
case Telephony.Sms.MESSAGE_TYPE_OUTBOX:
direction = "MISSED";
break;
default:
break;
}
Toast.makeText(this, phNumber + direction + callDuration + callDayTimes, Toast.LENGTH_SHORT).show();
}
}
c.close();
}
}