I'm writing an app for getting the latest call logs details from the Android mobile phone. I'm successfully receiving Last call details while running my app in Huawei mobiles. but if I run my app in Samsung mobiles I'm getting very oldest calls which means very last calls in the list. I'm using below method to getting last call logs details? What should I do for getting the latest last call details from the Samsung mobile?
private void getCallLogs() {
ContentResolver cr = ctx.getContentResolver();
if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Cursor c = cr.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
int totalCall = 1;
if (c != null) {
totalCall = 1; // 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 call_id = c.getString(c.getColumnIndexOrThrow(CallLog.Calls._ID));
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 callerName = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME));
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;
case CallLog.Calls.VOICEMAIL_TYPE:
direction = "VOICEMAIL_TYPE";
break;
case CallLog.Calls.REJECTED_TYPE:
direction = "REJECTED_TYPE";
break;
case CallLog.Calls.BLOCKED_TYPE:
direction = "BLOCKED_TYPE";
break;
case CallLog.Calls.ANSWERED_EXTERNALLY_TYPE:
direction = "ANS EXT TYPE";
break;
default:
break;
}
c.moveToPrevious(); // if you used moveToFirst() for first logs, you should this line to moveToNext
Toast.makeText(ctx, phNumber + callDuration + callDayTimes + direction +callerName , Toast.LENGTH_SHORT).show(); // you can use strings in this line
}
}
c.close();
}
}
Samsung Mobile Call Logs History saving to Cursor is different from other Mobile devices Like Huawei. So We have to find the device name and want run to separate method,
Example
String deviceMan = Build.MANUFACTURER;
String deviceName=deviceMan.toLowerCase();
if(deviceName.equals("samsung"))
{
getCallLogs("samsung");
}
else
{
getCallLogs("other");
}
if its Samsung device,
We have to use c.moveToFirst()
if (c != null) {
if(devicename.equals("samsung"))
{
if (c.moveToFirst()) { //starts pulling logs from last - you can use moveToFirst() for first logs
getCallDetails(c);
}
}else
{
if (c.moveToLast()) { //starts pulling logs from last - you can use moveToFirst() for first logs
getCallDetails(c);
}
}
c.close();
}