getContentResolver()。使用CallLog查询

时间:2014-12-13 23:14:39

标签: java android calllog

我正在开发Android应用。我需要获取通话记录以获取有关外拨电话的信息。我正在使用这个(来自这里的一些主题):

 private void getCallDetails() {

StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
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);
sb.append( "Call Details :");
while ( managedCursor.moveToNext() ) {
String phNumber = managedCursor.getString( number );
String callType = managedCursor.getString( type );
String callDate = managedCursor.getString( date );
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 = "INCOMING";
break;

case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append( "\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration );
sb.append("\n----------------------------------");
}
managedCursor.close();
call.setText(sb);
}

我也可以使用这个新代码:

Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI,null, null,null, null);

但是在这两个示例中,即使在模拟器上,我的应用也会在手机上崩溃。我试图修复,只是认为我发现这个查询行是问题,但只有CallLog。 当我使用与sms类似的代码时,例如每个思维工作正常。

public StringBuffer getOutgoingSMSContent() {

        ContentResolver contentResolver = getContentResolver();
        Uri uri = Uri.parse("content://sms/sent/");
        StringBuffer messagedata = new StringBuffer();
        int count = 0;

        Cursor cursor = contentResolver.query(uri, null, null, null, null);
        if (cursor.getCount() != 0) {
            if (cursor.moveToFirst()) {
                do {
                    messagedata.append("Outgoing message count: " + (count + 1)
                            + "\n");
                    for (int m = 0; m < cursor.getColumnCount(); m++) {
                        if (cursor.getColumnName(m).equalsIgnoreCase("address")
                                || cursor.getColumnName(m).equalsIgnoreCase(
                                        "date")
                                || cursor.getColumnName(m).equalsIgnoreCase(
                                        "body")
                                || cursor.getColumnName(m).equalsIgnoreCase(
                                        "type")) {
                            messagedata.append(cursor.getColumnName(m) + "  : "
                                    + cursor.getString(m));
                            messagedata.append("\n");
                        }
                    }
                    messagedata.append("\n");
                    count++;
                } while (cursor.moveToNext());
            }
        }
        cursor.close();
        cursor = null;
        return messagedata;
    }

有什么想法吗?

0 个答案:

没有答案