我已将联系人添加到我的地址簿中,该地址簿有多个,如下面的
我想使用ContactsContract内容uri获取所有3个“用户”.
通过使用下面的代码我只有一个联系人.
Cursor cursorAddressBook = mContentResolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); if (cursorAddressBook != null) { while (cursorAddressBook.movetoNext()) { String dataname = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.disPLAY_name)); String datanumber = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); int dataType = cursorAddressBook.getInt(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA2)); String contactID = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); Log.e("last updat name last", dataname); Log.e("last updated No last", datanumber); Log.e("last updated Type last", dataType); } cursorAddressBook.close(); }
解决方法:
从此blogpost如果联系人有多个电话号码,那么您可以使用AndroID中的AndroID内置类(Cursor和ContactsContract)检索所有电话号码和其他详细信息.您需要根据手机类型检索联系电话,如(TYPE_MOBILE,TYPE_HOME等)
{ Cursor cursor = cntx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts List if (contactsCount > 0) { while(cursor.movetoNext()) { String ID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String contactname = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.disPLAY_name)); if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { //the below cursor will give you details for multiple contacts Cursor pCursor = cntx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{ID}, null); // continue till this cursor reaches to all phone numbers which are associated with a contact in the contact List while (pCursor.movetoNext()) { int phoneType = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); //String isstarred = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED)); String phoneNo = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); //you will get all phone numbers according to it's type as below switch case. //Logs.e will print the phone number along with the name in DDMS. you can use these details where ever you want. switch (phoneType) { case Phone.TYPE_MOBILE: Log.e(contactname + ": TYPE_MOBILE", " " + phoneNo); break; case Phone.TYPE_HOME: Log.e(contactname + ": TYPE_HOME", " " + phoneNo); break; case Phone.TYPE_WORK: Log.e(contactname + ": TYPE_WORK", " " + phoneNo); break; case Phone.TYPE_WORK_MOBILE: Log.e(contactname + ": TYPE_WORK_MOBILE", " " + phoneNo); break; case Phone.TYPE_OTHER: Log.e(contactname + ": TYPE_OTHER", " " + phoneNo); break; default: break; } } pCursor.close(); } } cursor.close(); }}
总结 以上是内存溢出为你收集整理的获取与一个联系人android相关联的所有手机号码全部内容,希望文章能够帮你解决获取与一个联系人android相关联的所有手机号码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)