Android小米(miui)获取通话记录为null解决办法

Android小米(miui)获取通话记录为null解决办法

在开发中发现,查询通话记录时使用
contentResolver.query(
CallLog.Calls.CONTENT_URI, Columns, null,
null,CallLog.Calls.DATE+” desc”);
在小米真机上查询出来的结果为空

解决办法:

private List getRecordForMiui(){
        ContentResolver contentResolver = getContentResolver();
        List mRecordList = new ArrayList();
        //使用具体的路径而不是引用CallLog.Calls.CONTENT_URI
        Uri uri = Uri.parse("content://call_log/calls");
        Cursor cursor = null;
        try {
            cursor = contentResolver.query(
                    // CallLog.Calls.CONTENT_URI, Columns, null,
                    // null,CallLog.Calls.DATE+" desc");
                    uri, null, null, null,
                    CallLog.Calls.DATE + " desc");
            if (cursor == null)
                return null;

            while (cursor.moveToNext()) {
                RecordEntity record = new RecordEntity();
                record.name = cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.CACHED_NAME));
                record.number = cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.NUMBER));
                record.type = cursor.getInt(cursor
                        .getColumnIndex(CallLog.Calls.TYPE));
                record.lDate = cursor.getLong(cursor
                        .getColumnIndex(CallLog.Calls.DATE));
                record.duration = cursor.getLong(cursor
                        .getColumnIndex(CallLog.Calls.DURATION));
                record._new = cursor.getInt(cursor
                        .getColumnIndex(CallLog.Calls.NEW));
                Log.e("aaa", record.toString());
//                      int photoIdIndex = cursor.getColumnIndex(CACHED_PHOTO_ID);
//                      if (photoIdIndex >= 0) {
//                          record.cachePhotoId = cursor.getLong(photoIdIndex);
//                      }

                mRecordList.add(record);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            return mRecordList;
        }
    }

通话记录实体类

/**
 * Created by lxh on 2016/11/23.
 */

public class RecordEntity {
    String name;
    String number;
    int type;
    long lDate;
    long duration;
    int _new;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public long getlDate() {
        return lDate;
    }

    public void setlDate(long lDate) {
        this.lDate = lDate;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public int get_new() {
        return _new;
    }

    public void set_new(int _new) {
        this._new = _new;
    }
}

最后
注意添加权限

<uses-permission android:name="android.permission.READ_CALL_LOG"/>
    <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>

总结:
miui必须使用具体的uri路径

你可能感兴趣的:(Android)