三星手机通话记录详细信息以相反顺序保存到游标中

huangapple go评论106阅读模式
英文:

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 &lt; 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 = &quot;OUTGOING&quot;;
break;
case CallLog.Calls.INCOMING_TYPE:
direction = &quot;INCOMING&quot;;
break;
case CallLog.Calls.MISSED_TYPE:
direction = &quot;MISSED&quot;;
break;
case CallLog.Calls.VOICEMAIL_TYPE:
direction = &quot;VOICEMAIL_TYPE&quot;;
break;
case CallLog.Calls.REJECTED_TYPE:
direction = &quot;REJECTED_TYPE&quot;;
break;
case CallLog.Calls.BLOCKED_TYPE:
direction = &quot;BLOCKED_TYPE&quot;;
break;
case CallLog.Calls.ANSWERED_EXTERNALLY_TYPE:
direction = &quot;ANS EXT TYPE&quot;;
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(&quot;samsung&quot;))
{                
getCallLogs(&quot;samsung&quot;);
}
else
{
getCallLogs(&quot;other&quot;);
}

if its Samsung device,

We have to use c.moveToFirst()

if (c != null) {     
if(devicename.equals(&quot;samsung&quot;))
{
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();
}

huangapple
  • 本文由 发表于 2020年7月26日 18:29:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63098936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定