
我唯一知道的联系是给定的phonenumber,但对于我的功能,我需要有rawcontactID.我有一个工作代码,但现在我确实使用了2个单独的查询.我想要的是1个查询,可以同时保存一些查询时间.
我的代码:
Uri uri = Uri.withAppendedpath(Phone.CONTENT_FILTER_URI,Uri.encode(phoneNumber)); String[] columns = new String[]{Phone.CONTACT_ID,Phone.disPLAY_name,Phone.NUMBER,Phone._ID }; Cursor cursor = contentResolver.query(uri,columns,null,null); if(cursor!=null) { int clenght = cursor.getCount(); while(cursor.movetoNext()){ //contactname = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.disPLAY_name)); ID = cursor.getString(cursor.getColumnIndex(Phone.CONTACT_ID)); } cursor.close(); }Cursor pCur = contentResolver.query(ContactsContract.Data.CONTENT_URI,new String[]{ContactsContract.Data.RAW_CONTACT_ID},ContactsContract.Data.CONTACT_ID+" = "+ ID,null); if(pCur!=null) { int clenght = pCur.getCount(); while(pCur.movetoNext()){ //contactname = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.disPLAY_name)); ID = pCur.getString(pCur.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID)); } pCur.close(); } 提前致谢
编辑:
我上面的代码工作正常,但我仍在寻找大量联系人的增加速度.因此,如果有人提供解决方案来结合我的查询,我会给予赏金.
解决方法private String[] getRawContactIDFromNumber(String givennumber){ List<String> rawIDs = new ArrayList<String>(); Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID},ContactsContract.CommonDataKinds.Phone.NUMBER + "='"+ givennumber +"'",ContactsContract.CommonDataKinds.Phone.NUMBER); while (phones.movetoNext()) { rawIDs.add( phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID))); Log.v("contacts","Given Number: " + givennumber + "Raw ID: " +rawIDs.get(rawIDs.size() - 1)); } phones.close(); String[] ret = new String[0]; return rawIDs.toArray(ret); } 编辑为仅包含光标中的原始ID以提高效率.如果多个联系人具有相同的号码,也会将返回类型更改为数组.
总结以上是内存溢出为你收集整理的Android查询phonenumber以获取rawcontactID全部内容,希望文章能够帮你解决Android查询phonenumber以获取rawcontactID所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)