码迷,mamicode.com
首页 > 其他好文 > 详细

访问通讯录

时间:2015-08-20 18:39:56      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

设置访问通讯录权限

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

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

String str="";

        Uri uri=Uri.parse("content://com.android.contacts/contacts");

        ContentResolver resolver=getApplicationContext().getContentResolver();

        Cursor cursor=resolver.query(uri, new String[]{"_id"}, null, null, null);

        while(cursor.moveToNext()){

            int contactid=cursor.getInt(0);

            str+="contentID:";

            str+=contactid;

            Uri uri2=Uri.parse("content://com.android.contacts/contacts/"+contactid+"/data");

            Cursor cursor2=resolver.query(uri2, null, null, null, null);

            //String[] strings=cursor2.getColumnNames();

            //cursor2.moveToFirst();

            while(cursor2.moveToNext()){

                String data=cursor2.getString(cursor2.getColumnIndex("data1"));

                String type=cursor2.getString(cursor2.getColumnIndex("mimetype"));



                if("vnd.android.cursor.item/name".equals(type)){

                    //姓名

                    str+=",name="+data;

                }else if("vnd.android.cursor.item/phone_v2".equals(type)){

                    //电话数据

                    str+=",phone="+data;

                }else if("vnd.android.cursor.item/email_v2".equals(type)){

                    //电话数据

                    str+=",email="+data;

                }

                

            }

            Log.i("TAG", str);

            str="";

        }

        Toast.makeText(getApplicationContext(), "success", 1).show();

 

访问通讯录

标签:

原文地址:http://www.cnblogs.com/zhangjie9142/p/4745790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!