Reputation: 329
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();
}
}
Upvotes: 4
Views: 10686
Reputation: 755
Please add this permission in your manifest file.
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
Add this function in your Activity file and call it in oncreate function.
public void getCallLogs() {
int flag=1;
title.setText(Html.fromHtml("<b>Call Logs</b>"));
deviceDetails.setText(Html.fromHtml(""));
StringBuilder callLogs = new StringBuilder();
ArrayList<String> calllogsBuffer = new ArrayList<String>();
calllogsBuffer.clear();
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);
while (managedCursor.moveToNext()) {
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:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
calllogsBuffer.add("\nPhone Number: " + phNumber + " \nCall Type: "
+ dir + " \nCall Date: " + callDayTime
+ " \nCall duration in sec : " + callDuration + "\n");
}
managedCursor.close();
}
To call this function, add this to your oncreate function in your Activity.
getCallLogs();
Upvotes: 6
Reputation: 329
Here we can create an integer with the for loop that we have already created and set a limit here.
I'm sorry about my bad English.
Function;
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 = 10; // intenger call log limit
if (c.moveToLast()) { //starts pulling logs from last - you can use moveToFirst() for first logs
for (int j = 0; j < totalCall; j++) {
String phNumber = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
String callDate = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DATE));
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 CallLog.Calls.OUTGOING_TYPE:
direction = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
direction = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
direction = "MISSED";
break;
default:
break;
}
c.moveToPrevious(); // if you used moveToFirst() for first logs, you should this line to moveToNext
Toast.makeText(getBaseContext(), phNumber + callDuration + callDayTimes + direction, Toast.LENGTH_SHORT).show(); // you can use strings in this line
}
}
c.close();
}
}
Upvotes: 3