android实现读取、搜索联系人的代码
代码很简单,就不多废话了
//读取联系人 publicstaticUriCONTACTSURI=ContactsContract.Contacts.CONTENT_URI;//联系人 publicstaticvoidgetContactsInfo(Contextcontext,Stringtag){ String[]projections=newString[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME}; Cursorcursor=context.getContentResolver().query(CONTACTSURI,projections,null,null,null); intalbumIndex=cursor.getColumnIndexOrThrow(projections[0]); inttitleIndex=cursor.getColumnIndexOrThrow(projections[1]); Log.d(tag,cursor.getCount()+""); while(cursor.moveToNext()){ Stringalbum=cursor.getString(albumIndex); Stringtitle=cursor.getString(titleIndex); Log.d(tag,album+":"+title); } cursor.close(); } //根据联系人搜索联系人信息 publicstaticvoidsearchContacts(Contextcontext,Stringtag){ StringsearchName="Wang"; Uriuri=Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,searchName); // Uriuri2=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,phoneNumber);根据电话号码查找联系人 String[]projection=newString[]{ContactsContract.Contacts._ID}; Cursorcursor=context.getContentResolver().query(uri,projection,null,null,null); Stringid=null; if(cursor.moveToFirst()){ id=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); } cursor.close(); if(id!=null){ Stringwhere=ContactsContract.Data._ID+"="+id; projection=newString[]{ContactsContract.Data.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER}; CursorsearchcCursor=context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,projection,where,null,null); Log.d(tag,searchcCursor.getCount()+""); intnameIndex=searchcCursor.getColumnIndex(projection[0]); intnumberIndex=searchcCursor.getColumnIndex(projection[1]); while(searchcCursor.moveToNext()){ Stringname=searchcCursor.getString(nameIndex); Stringnumber=searchcCursor.getString(numberIndex); Log.d(tag,number+":"+name); } searchcCursor.close(); } }
以上就是本文给大家分享的代码的全部内容了,希望大家能够喜欢。