访问通讯录

设置访问通讯录权限

<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();

 

posted @ 2015-08-20 17:11  Jason_Msbaby  阅读(133)  评论(0编辑  收藏  举报