利用ContentResolver内容访问者获取手机联系人信息

来源:互联网 发布:linux7安装mysql 编辑:程序博客网 时间:2024/04/30 09:08
        利用ContentResolver内容访问者,获取手机联系人信息我做了两种不同的做法。第一种,直接获取所有手机联系人信息,展示在ListView中。第二种,通过Butten按钮跳转到系统的手机联系人界面,单个获取手机联系人信息,展示在ListView中,结果如下:

第一种:
这里写图片描述

第二种:

这里写图片描述

第一种:直接获取所有手机联系人信息首先需要在AndroidManifest.xml文件中添加权限:    <uses-permission android:name="android.permission.READ_CONTACTS" />    activity_main.xml布局:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       xmlns:tools="http://schemas.android.com/tools"        android:id="@+id/activity_main"       android:layout_width="match_parent"       android:layout_height="match_parent"       android:orientation="vertical"       tools:context="com.example.android_25.MainActivity">        <ListView           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:id="@+id/lv_lxr"           >       </ListView>   </LinearLayout>
    activity_xs.xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        android:id="@+id/activity_xs"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        tools:context="com.example.android_25.XsActivity">    <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:id="@+id/tv_name"        />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:id="@+id/tv_telephone"        />    </LinearLayout>
    MainActivity类:
private ListView lv_lxr;private Button b_name;private ContentResolver cr;private List<Map<String, Object>> datalistView;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //获得ListView    lv_lxr = (ListView) findViewById(R.id.lv_lxr);    //得到访问者    cr = getContentResolver();    //定义一个接收联系人姓名和电话号码的集合    datalistView = new ArrayList<>();            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"));                Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");                Cursor contactData= cr.query(uriData,null,null,null,null);                //用来装姓名                String aa="";                //用来装号码                String bb="";                while(contactData.moveToNext()){                    String type=contactData.getString(contactData.getColumnIndex("mimetype"));                    //如果获取的是vnd.android.cursor.item/phone_v2则是号码                    if(type.equals("vnd.android.cursor.item/phone_v2")){                        bb=contactData.getString(contactData.getColumnIndex("data1"));                        //如果获取的是vnd.android.cursor.item/name则是姓名                    }else if(type.equals("vnd.android.cursor.item/name")) {                        aa=contactData.getString(contactData.getColumnIndex("data1"));                    }                }                //将用户名和号码放入Map集合中                Map<String,Object> map=new HashMap<>();                map.put("images",aa);                map.put("titles",bb);                datalistView.add(map);            }    SimpleAdapter adapter=new SimpleAdapter(this, datalistView,R.layout.activity_xs,new String[]{"images","titles"},new int[]{R.id.tv_name,R.id.tv_telephone});    lv_lxr.setAdapter(adapter);}

第二种:通过Butten按钮跳转到系统的手机联系人界面,单个获取手机联系人信息,展示在ListView中

activity_contacts.xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"            xmlns:tools="http://schemas.android.com/tools"            android:id="@+id/activity_contacts"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical"            tools:context="com.example.android_25.ContactsActivity">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content">                <Button                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="跳转到联系人页面"                    android:id="@+id/b_tzcontacts"                    />        </LinearLayout>        <ListView            android:layout_width="wrap_content"            android:layout_height="wrap_content"                android:id="@+id/lv_contacts"            ></ListView></LinearLayout>

ContactsActivity类:

private Button b_tzcontacts;private String phoneName;private String phoneNumber;private List<Map<String,Object>> datalistView;private ListView lv_contacts;private SimpleAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_contacts);    //获得跳转到联系人的id    b_tzcontacts =(Button) findViewById(R.id.b_tzcontacts);    //获得ListView的ID    lv_contacts =(ListView) findViewById(R.id.lv_contacts);    //定义一个接受联系人姓名和电话号码的集合    datalistView = new ArrayList<>();    //获取联系人的点击事件    b_tzcontacts.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            Intent intentPhone=new Intent(Intent.ACTION_PICK);            intentPhone.setData(ContactsContract.Contacts.CONTENT_URI);            startActivityForResult(intentPhone,0);        }    });    //R.layout.activity_xs就是上文的activity_xs布局问价    adapter = new SimpleAdapter(this, datalistView, R.layout.activity_xs,new String[]{"images","titles"},new int[]{R.id.tv_name,R.id.tv_telephone});    lv_contacts.setAdapter(adapter);}    //获得返回的结果@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    switch (requestCode){        case 0:            if(resultCode== Activity.RESULT_OK){                Uri uri=data.getData();                Cursor cursor=managedQuery(uri,null,null,null,null);                cursor.moveToFirst();                String contactid=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));                //得到ContentResolver                ContentResolver contentResolver=getContentResolver();                Cursor phone=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactid,null,null);                while (phone.moveToNext()){                    //联系人                    phoneName =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));                    //手机号码                    phoneNumber =phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                    //格式化手机号                    phoneNumber = phoneNumber.replace("-","");                    phoneNumber = phoneNumber.replace("","");                    //将用户名和号码放入Map集合中                    Map<String,Object> map=new HashMap<>();                    map.put("images",phoneName);                    map.put("titles",phoneNumber);                    datalistView.add(map);                }                //刷新适配器                adapter.notifyDataSetChanged();            }            break;    }}
0 0