6

在我的应用程序中,我使用此代码获取所有通话记录。它返回我的 android 手机中的所有通话记录。

public class CallLogHelper {

    public static Cursor getAllCallLogs(ContentResolver cr) {
        // reading all data in descending order according to DATE
        String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
        Uri callUri = Uri.parse("content://call_log/calls");
        Cursor curCallLogs = cr.query(callUri, null, null, null, strOrder);
        return curCallLogs;
    }

    public static void insertPlaceholderCall(ContentResolver contentResolver,
            String name, String number) {
        ContentValues values = new ContentValues();
        values.put(CallLog.Calls.NUMBER, number);
        values.put(CallLog.Calls.DATE, System.currentTimeMillis());
        values.put(CallLog.Calls.DURATION, 0);
        values.put(CallLog.Calls.TYPE, CallLog.Calls.OUTGOING_TYPE);
        values.put(CallLog.Calls.NEW, 1);
        values.put(CallLog.Calls.CACHED_NAME, name);
        values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
        values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
        Log.d("Call Log", "Inserting call log placeholder for " + number);
        contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
    }

}

但我的问题是我想从特定日期获取通话记录,而不是所有通话记录。我不知道如何使用查询从特定日期获取通话记录。帮帮我谢谢

4

2 回答 2

6
StringBuffer sb = new StringBuffer();
    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
    Uri callUri = Uri.parse("content://call_log/calls");

    Calendar calendar = Calendar.getInstance();

    calendar.set(2014, Calendar.MAY, 25);
    String fromDate = String.valueOf(calendar.getTimeInMillis());
    calendar.set(2014, Calendar.MAY, 30);
    String toDate = String.valueOf(calendar.getTimeInMillis());
    String[] whereValue = {fromDate,toDate};


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

    //Cursor cur = cr.query(callUri, null, android.provider.CallLog.Calls.DATE+" >= ?", whereValue, strOrder);
    // loop through cursor
    while (cur.moveToNext()) {
        String callNumber = cur.getString(cur
                .getColumnIndex(android.provider.CallLog.Calls.NUMBER));

        String callName = cur
                .getString(cur
                        .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));

        String callDate = cur.getString(cur
                .getColumnIndex(android.provider.CallLog.Calls.DATE));
        SimpleDateFormat formatter = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm");
        String dateString = formatter.format(new Date(Long
                .parseLong(callDate)));

        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: 
            dir = "OUTGOING"; 
                break; 
        case CallLog.Calls.INCOMING_TYPE:
            dir = "INCOMING"; 
                break; 
        case CallLog.Calls.MISSED_TYPE: 
            dir = "MISSED"; 
                break; 
    } 

        String isCallNew = cur.getString(cur
                .getColumnIndex(android.provider.CallLog.Calls.NEW));

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

        sb.append("\nPhone Number:--- " + callNumber + " \nName:--- "+ callName +" \nCall Type dir:--- " + dir + " \nCall Date:--- " + dateString + " \n duration in sec :--- " + duration);
        sb.append("\n----------------------------------"); 

    }

返回的通话记录介于 25/05/2014 和 30/05/2014 之间。返回值全部存储在 sb 变量中,您可以在 TextView 中设置该值进行测试

于 2014-06-03T09:08:34.567 回答
5
        String[] strFields = {
                android.provider.CallLog.Calls.NUMBER, 
                android.provider.CallLog.Calls.TYPE,
                android.provider.CallLog.Calls.CACHED_NAME,
                android.provider.CallLog.Calls.DATE,
                android.provider.CallLog.Calls.DURATION,

                };

        // Defines a string to contain the selection clause
        String mSelectionClause = android.provider.CallLog.Calls.DATE+ " >= ?";

        // Initializes an array to contain selection arguments
        String[] mSelectionArgs = { createDate(2013,1,1).toString() };

        Cursor mCallCursor = currentContext.getContentResolver().query(
                android.provider.CallLog.Calls.CONTENT_URI,
                strFields,
                mSelectionClause,
                mSelectionArgs,
                null
                );

        return mCallCursor;

public static Long createDate(int year, int month, int day)
{
    Calendar calendar = Calendar.getInstance();

    calendar.set(year, month, day);

    return calendar.getTimeInMillis();

}
于 2013-02-02T15:41:33.883 回答