关于getContentResolver()查询CallLog

时间:2012-06-14 10:19:39

标签: android calllog resolver

    Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
    for (int i = 0; i < c.getColumnCount(); i++) {
        Log.i(getClass().getName(), "retrieveCall(): " + c.getColumnName(i));
    }

我可以在4.0.x中获取所有colume名称,但只能在4.0.x下获得_id。我的代码有什么问题? Thx提前!

    Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
    for (int i = 0; i < c.getColumnCount(); i++) {
        Log.i(getClass().getName(), "retrieveCall(): " + c.getColumnName(i));
    }

    while (c.moveToNext()) {
        for (int i = 0; i < c.getColumnCount(); i++) {
            Log.i(getClass().getName(), "retrieveCall(): " + c.getColumnName(i) + " = " + c.getInt(i) + "/" + c.getString(i));
        } ...

上面的代码在4.0.x中运行良好,我猜数据库有一些差异吗?

@Anu,这是我的完整代码,如果您发现错误,请告诉我:

private void retrieveCall()
{
    Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);

    if (c != null) {
        while (c.moveToNext()) {
            String number = c.getString(c.getColumnIndex("number"));
            String name = c.getString(c.getColumnIndex("name"));
            long date = c.getLong(c.getColumnIndex("date"));

            if (number.length() > 0) {
                LogDetail log = null;
                if (_callTable.containsKey(number)) {
                    log = (LogDetail) _callTable.get(number);
                    log.name = name;
                    log.date = date;
                    log.amount++;
                } else {
                    log = new LogDetail();
                    log.name = name;
                    log.date = date;
                    log.amount = 1;
                }
                _callTable.put(number, log);
            }
        }
        c.close();
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个......它对我有用......

   Cursor c1 = getContentResolver().query(CallLog.Calls.CONTENT_URI,null,null,null,null); 
  for(int i=0;i<c1.getColumnCount();i++){
    Log.i("Column name", ""+c1.getColumnName(i));
         }   

答案 1 :(得分:1)

不要忘记移动Cursor

的位置

使用:

Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
while (c.moveToNext()) {
    Log.i(getClass().getName(), "retrieveCall(): " + c.getColumnName(i));
}