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

Samsung's mobiles call logs details saving to Cursor in reverse order

我正在编写一个应用程序,用于从 Android 手机 phone 获取最新的通话记录详细信息。 运行在华为手机中使用我的应用程序时,我成功收到了最后一次通话的详细信息。但是如果我 运行 我在三星手机上的应用程序,我会收到最旧的电话,这意味着列表中的最后一个电话。我正在使用以下方法获取上次通话记录的详细信息吗?我应该如何从三星手机获取最新的最后一次通话详情?

  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 保存到 Cursor 与华为等其他移动设备不同。所以我们必须找到设备名称并想要 运行 单独的方法,

Example

            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()) { //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();
        }