Android呼叫两个日期之间的日志数据和来自相同号码的呼叫总持续时间。

时间:2016-02-04 17:30:20

标签: android android-contentprovider duration android-cursor calllog

希望在今天,昨天,过去七天和过去30天之前获取通话记录历史记录以及我希望显示该特定号码的来电和去电的总持续时间。

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

请告诉我们是否可以获取持续时间并按光标 GroupBy ORDER 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));

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

2 个答案:

答案 0 :(得分:4)

我设法在两个日期之间获取通话记录。您可以获取今天,昨天,过去七天,最近30天的通话记录。以及呼叫的持续时间(同一号码的呼叫也是多次)

你应该将选择作为
  android.provider.CallLog.Calls.DATE +“BETWEEN?AND?”

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

atlast将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());


            }
        }

希望这对观众有所帮助。

答案 1 :(得分:0)

我设法在两个日期之间获取通话记录,您也可以获得通话的持续时间。尝试此方法..

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);

}

}