【问题标题】:about getContentResolver() query CallLog关于getContentResolver()查询CallLog
【发布时间】:2012-06-17 08:47:30
【问题描述】:
    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 中获取所有列名,但在 4.0.x 下只能获取 _id。我的代码怎么了?提前谢谢!

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

【问题讨论】:

  • 代码没问题??你在哪个平台上苦苦挣扎?

标签: android calllog resolver


【解决方案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));
}

【讨论】:

  • @lon 他只想打印列名。所以不需要 c.moveToNext()。
  • 对不起,我没有很好地解释自己。我确实调用了 moveToNext(),仍然没有结果。
【解决方案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));
         }   

【讨论】:

  • 即使你的代码工作正常......你不需要包含 while 循环代码,只有代码 sn-p 才能工作
  • 为您工作?我粘贴您的代码并运行并打印,但只得到一个 _id。是安卓的bug吗?我在 2.3.5 中测试了这些代码://? { stackoverflow.com/questions/11031067/… 光标 c1 = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null); for (int i = 0; i
  • 对不起,是我分化系统的问题。我使用 SQLite Manager 发现,数据库名称不是“contact.db”而是“contact2.db”!虽然 ClearMaster 仍然可以正常工作,但我认为我应该暂时忽略它。
猜你喜欢
  • 1970-01-01
  • 2017-12-01
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
  • 2012-03-05
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多