英文:
Samsung's mobiles call logs details saving to Cursor in reverse order
问题
我正在编写一个用于从安卓手机获取最新通话记录详细信息的应用程序。在华为手机上运行我的应用程序时,我成功地接收到最后通话的详细信息。但是如果我在三星手机上运行我的应用程序,我得到的是非常旧的通话记录,也就是列表中的最后通话记录。我正在使用以下方法获取最后通话记录的详细信息?我应该如何在三星手机上获取最新的通话记录呢?
private void getCallLogs() {
ContentResolver cr = ctx.getContentResolver();
if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
return;
}
Cursor c = cr.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
int totalCall = 1;
if (c != null) {
totalCall = 1; // 通话记录限制数量
if (c.moveToLast()) { // 从最后开始提取日志 - 你可以使用 moveToFirst() 提取第一个日志
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(); // 如果你使用 moveToFirst() 提取第一个日志,你需要将此行更改为 moveToNext
Toast.makeText(ctx, phNumber + callDuration + callDayTimes + direction +callerName , Toast.LENGTH_SHORT).show();
}
}
c.close();
}
}
英文:
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();
}
}
答案1
得分: 0
三星手机的通话记录保存到Cursor中的方式与其他手机设备(如华为)不同。因此,我们需要找到设备名称并运行不同的方法,
> 示例
String deviceMan = Build.MANUFACTURER;
String deviceName = deviceMan.toLowerCase();
if (deviceName.equals("samsung")) {
getCallLogs("samsung");
} else {
getCallLogs("other");
}
如果是三星设备,
我们需要使用 `c.moveToFirst()`
if (c != null) {
if (deviceName.equals("samsung")) {
if (c.moveToFirst()) { // 从最后开始提取日志 - 您可以使用 moveToFirst() 来提取第一条日志
getCallDetails(c);
}
} else {
if (c.moveToLast()) { // 从最后开始提取日志 - 您可以使用 moveToFirst() 来提取第一条日志
getCallDetails(c);
}
}
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论