其他分享
首页 > 其他分享> > android-关于getContentResolver()查询CallLog

android-关于getContentResolver()查询CallLog

作者:互联网

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

解决方法:

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

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

标签:resolver,calllog,android
来源: https://codeday.me/bug/20191201/2079025.html