ContentResolver(内容访问者)访问通讯录

访问通讯录可以分为两种方式,第一种是直接调用系统通讯录的界面直接访问,第二种是自己写一个界面,从系统通讯录里面拿到数据,再绑定在自己写的界面。

今天我们用第二种方法来实现访问通讯录

1,访问通讯录首先要写访问通讯录的权限


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

2,拿到内容访问者

 //得到内容访问者
        ContentResolver cr= getContentResolver();

3,用内容访问者去点 query 方法,返回类型是Cursor

Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
Cursor cursor=cr.query(uri,null,null,null,null);

4,得到ID和名字

 while (cursor.moveToNext()){
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String name = cursor.getString(cursor.getColumnIndex("display_name"));
//            Log.i("text",""+id+"   "+name);
}

5,根据联系人的ID获取联系人相对应的数据(名字+phone)

Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
            Cursor cursor1=cr.query(uriData,null,null,null,null);
            while(cursor1.moveToNext()){
                //号码
                String data1=cursor1.getString(cursor1.getColumnIndex("data1"));
                //int type=cursorData.getInt(cursorData.getColumnIndex("mimetype_id"));
                String type=cursor1.getString(cursor1.getColumnIndex("mimetype"));
                if("vnd.android.cursor.item/phone_v2".equals(type)){
                    Log.i("text","   "+data1+"    "+ name);
                }
            }

然后自己在写一个ListView把数据放进去就OK了

package com.zking.hgz_android_25_contentproviter_02;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class Contacts extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);
        ListView lv_contacts= (ListView) findViewById(R.id.lv_contacts);

        //得到内容访问者
        ContentResolver cr= getContentResolver();
        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("text",""+id+"   "+name);

            //根据联系人的ID获取联系人相对应的数据(名字+phone)
            Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
            Cursor cursor1=cr.query(uriData,null,null,null,null);
            while(cursor1.moveToNext()){
                //号码
                String data1=cursor1.getString(cursor1.getColumnIndex("data1"));
                //int type=cursorData.getInt(cursorData.getColumnIndex("mimetype_id"));
                String type=cursor1.getString(cursor1.getColumnIndex("mimetype"));
                if("vnd.android.cursor.item/phone_v2".equals(type)){
                    Log.i("text","   "+data1+"    "+ name);
                }
            }
//            SimpleCursorAdapter adapter = new SimpleCursorAdapter(Contacts.this, R.layout.listview_item, cursor, new String[]{"display_name"}, new int[]{R.id.load2_listview_item_textview});
//            lv_contacts.setAdapter(adapter);
        }
    }
}

你可能感兴趣的:(ContentResolver(内容访问者)访问通讯录)