Android 两个日期之间的通话记录数据和来自同一号码的通话总时长。

Android Call Log data between two dates and Total duration of calls from same number.

想要获取今天、昨天、过去 7 天和过去 30 天的通话记录历史记录,同时我想显示该特定号码的来电和去电的总持续时间。

假设abc有3个去电和1个来电。我应该得到这些电话的总持续时间。

让我知道我们是否可以通过游标 GroupByORDER BY 子句而不是循环和添加持续时间来获取持续时间和调用日志.给我粗略的结构以获得更好的解决方案并且可以有效地工作。

         String[] whereValue=null;
        Calendar calendar = Calendar.getInstance();
        String currentDate = String.valueOf(calendar.getTimeInMillis());
        switch (period) {
            case DAY:
                whereValue  = new String[]{getTimeFrom(period),currentDate};
                break;
            case YESTERDAY:
                whereValue  = new String[]{getTimeFrom(period),currentDate};
                break;
            case WEEK:
                whereValue  = new String[]{getTimeFrom(period),currentDate};

                break;
            case MONTH:
                    whereValue  = new String[]{getTimeFrom(period),currentDate};
                break;
            default:
                Log.d(Utils.LOG_TAG, "Error:");
        }
        String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
        Uri callUri = Uri.parse("content://call_log/calls");

        Cursor cur = context.getContentResolver().query(callUri, null, android.provider.CallLog.Calls.DATE+" BETWEEN ? AND ?", whereValue, strOrder);

   String callNumber = cur.getString(cur
                    .getColumnIndex(android.provider.CallLog.Calls.NUMBER));

            String callName = cur
                    .getString(cur
                            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
      String callType = cur.getString(cur
                    .getColumnIndex(android.provider.CallLog.Calls.TYPE));
            String dir = null;
            int dircode = Integer.parseInt(callType);
            switch (dircode) {
                case CallLog.Calls.OUTGOING_TYPE:
                case CallLog.Calls.INCOMING_TYPE:
                    break;

            }
            String duration = cur.getString(cur
                    .getColumnIndex(android.provider.CallLog.Calls.DURATION));

以上代码甚至无法获取从日期到日期之间的通话记录。有帮助吗?

我已经设法获取了两个日期之间的通话记录。您可以获得今天、昨天、过去 7 天、过去 30 天的通话记录。以及通话时长(也有多次来自同一号码的通话)

你应该通过选择作为
android.provider.CallLog.Calls.DATE + "介于?和?"

selectionArgs

whereValue = new String[]{String.valueOf(calendar1.getTimeInMillis()),String.valueOf(calendar.getTimeInMillis());};

  Map<String, StatEntry> callLogMap1 = new HashMap<>();
            callLogMap1.clear();

            String strOrder1 = android.provider.CallLog.Calls.DATE + " DESC limit 500";

            Uri callUri = Uri.parse("content://call_log/calls");
            Cursor cur = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE + " BETWEEN ? AND ?", whereValue,
                    strOrder1);


            if (cur != null) {
                try {
                    while (cur.moveToNext()) {

                        String callNumber = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
                        //    String callDate = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.DATE));
                        int duration = cur.getInt(cur.getColumnIndex(android.provider.CallLog.Calls.DURATION));

                        String name = cur.getString(cur.getColumnIndex(CallLog.Calls.CACHED_NAME));

                        StatEntry StatEntry1 = null;

                        int id = cur.getInt(cur.getColumnIndex(CallLog.Calls._ID));

                        int type = cur.getInt(cur.getColumnIndex(CallLog.Calls.TYPE));

                        if (callNumber != null & duration > 0 && (type == 1 || type == 2)) {

                            int n = callNumber.length();
                            String lastDigits;
String number = callNumber.replaceAll(Pattern.quote("+"), ""); //replacing the plus
    //am just checking last 5digitis and saving to map so that we can get same //number duration
                            if (n >= 5) {

                                try {

                                    lastDigits = String.valueOf(Long.parseLong(number) % 100000);
                                } catch (NumberFormatException e) {
                                    e.printStackTrace();
                                    lastDigits = callNumber;
                                }
                            } else {
                                lastDigits = callNumber;
                            }

                            if (callLogMap1.containsKey(lastDigits)) {
                                StatEntry1 = callLogMap1.get(callNumber);

                                    StatEntry1.setTitle(callNumber);
                                StatEntry1.Duration += duration;


                            } else {
                                StatEntry1 = new StatEntry();

                  StatEntry1.setTitle(callNumber);

      StatEntry1.Duration += duration;
                           }


                    StatEntry1.setTime((StatEntry1.Duration) / 60);


                            callLogMap1.put(callNumber, StatEntry1);


                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace(

                    );
                } finally {
                    cur.close();
                }
            }

最后将 hashmap 数据传递给 arraylist。

ArrayList<StatEntry> callLogList1 = new ArrayList<>(callLogMap1.size());

        if (callLogMap1.size() > 0) {
            for (Map.Entry<String, StatEntry> entry : callLogMap1.entrySet()) {
                callLogList1.add(entry.getValue());


            }
        }

希望这对观众有所帮助。

我设法获取了两个日期之间的通话记录,您还可以获取 calls.Try 此方法的持续时间..

public class CallLogActivity extends Activity {


TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_log);
    textView = (TextView) findViewById(R.id.textCallBetween);
    textView.setVisibility(View.GONE);
   // listcallLog = (ListView) findViewById(R.id.calllogItems);

    getCalldetails();


}

public void getCalldetails() {
    StringBuffer stringBuffer = new StringBuffer();

    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

    Calendar calender = Calendar.getInstance();


    calender.set(2016, calender.NOVEMBER, 18);
    String fromDate = String.valueOf(calender.getTimeInMillis());

    calender.set(2016, calender.NOVEMBER, 20);
    String toDate = String.valueOf(calender.getTimeInMillis());

    String[] whereValue = {fromDate,toDate};


    if (ActivityCompat.checkSelfPermission(this, 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 managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE + " BETWEEN ? AND ?", whereValue, strOrder);

//   Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE, new String[]{" BETWEEN ? AND ?"}, strOrder);
    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);

    stringBuffer.append("Call Log :");

    while (managedCursor.moveToNext())
    {
        String phoneNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        SimpleDateFormat formatter = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm");
        String dateString = formatter.format(new Date(Long
                .parseLong(callDate)));
      //  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 = "INCOMMING";
                break;
            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED CALL";
                break;

        }

        stringBuffer.append("\nPhone Number:--- " + phoneNumber + "\nCall Type:--- "
                + dir + "\nCall Date:---"
                + dateString + "\nCall Duration:---" + callDuration);
        stringBuffer.append("\n--------------------------");

    }

    textView.setText(stringBuffer);
    textView.setVisibility(View.VISIBLE);

}

}