安卓 ContentProvider(获取系统联系人)

今天来记录一下怎么拿到系统联系人的信息。做了一个简单的demo,接下来就直接贴代码:
首先需要获得内容访问者:

cr = getContentResolver();

得到联系人的id及名字:

  Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        Cursor cursor = cr.query(uri, null, null, null, null);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String name = cursor.getString(cursor.getColumnIndex("display_name"));
            Log.i("test", id + " " + name);

还可以根据联系人的id获取联系人相对应的数据:

 Uri uri1 = Uri.parse("content://com.android.contacts/raw_contacts/" + id + "/data");
            Cursor cursor1 = cr.query(uri1, null, null, null, null);

            while (cursor1.moveToNext()) {
                String data1 = cursor1.getString(cursor1.getColumnIndex("data1"));
                String mimetype = cursor1.getString(cursor1.getColumnIndex("mimetype"));
                Log.i("test", data1 + " " + mimetype);
            }

    }

但别忘了配置权限:

 <uses-permission android:name="android.permission.READ_CONTACTS">uses-permission>

你可能感兴趣的:(安卓,android)