我通过
成功获取了联系人 val cursor = context.contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null)
但是在我同步后,当我的联系人列表中没有任何联系人并且从网络端在 google帐户中添加联系人时,就会出现问题从这个 xxx @ gmail帐户用我的手机中的联系人,所有联系人会在我的联系人列表中显示,但是当我尝试获取联系人然后,它返回0 。
我不知道问题出在哪里,还有其他方法可以获取此联系人!
如果我插入的新联系人比我从查询上方获得的联系人多,但找不到使用 google在网络面板中添加的联系人帐户。
如果遇到此类问题,请帮助我。
我正在使用以下代码来获取联系人
val cursor = context.contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null)
val projection = arrayOf(ContactsContract.Contacts._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.PHOTO_URI, ContactsContract.CommonDataKinds.Phone.TYPE)
val alContacts = ArrayList<Contact>()
var mMapContact = JSONObject()
if (cursor!!.moveToFirst()) {
do {
val id = cursor!!.getString(cursor!!.getColumnIndex(ContactsContract.Contacts._ID))
if (Integer.parseInt(cursor!!.getString(cursor!!.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
val pCur = context.contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", arrayOf<String>(id), ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC")
var mListMobile = ArrayList<MobileType>()
var mNameContact = ""
while (pCur!!.moveToNext()) {
val number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
mNameContact = pCur!!.getString(pCur!!.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
val type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))
when (type) {
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE -> {
mListMobile.add(MobileType(number, 0))
mMapContact.put(number.replace(Regex(CONTACT_PLUS_PATH_PREFIX), ""), mNameContact)
}
ContactsContract.CommonDataKinds.Phone.TYPE_HOME -> {
mListMobile.add(MobileType(number, 1))
mMapContact.put(number.replace(Regex(CONTACT_PLUS_PATH_PREFIX), ""), mNameContact)
}
ContactsContract.CommonDataKinds.Phone.TYPE_WORK -> {
mListMobile.add(MobileType(number, 2))
mMapContact.put(number.replace(Regex(CONTACT_PLUS_PATH_PREFIX), ""), mNameContact)
}
}
/*val contactNumber = pCur!!.getString(pCur!!.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
val contactName = pCur!!.getString(pCur!!.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
val contactInfo = ContactInfo(""+id, ""+contactName, "", ""+contactNumber)
LogUtil.e(TAG, "getContactList: $contactNumber - $contactName - $id")
alContacts.add(contactInfo)
break*/
}
pCur?.close()
if(mListMobile.size > 0) {
var contact : Contact? = null
for (mobile in mListMobile) {
contact = Contact(id, "" + mNameContact, ""+mNameContact, false, null, ""+mobile.mobile, "", mobile.type)
alContacts.add(contact!!)
}
}
}
} while (cursor?.moveToNext())
}
答案 0 :(得分:1)
您可以按以下方式使用:
#define BOOST_TEST_NO_MAIN true
希望它会为您提供帮助。
答案 1 :(得分:1)
我从@Rakesh答案中解决了我的问题,以便在Kotlin中获得更好的结果,您可以像这样使用
val projection = arrayOf(ContactsContract.Data.MIMETYPE, ContactsContract.Data.CONTACT_ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_URI, ContactsContract.Contacts.STARRED, ContactsContract.RawContacts.ACCOUNT_TYPE, ContactsContract.CommonDataKinds.Contactables.DATA, ContactsContract.CommonDataKinds.Contactables.TYPE)
val selection = ContactsContract.Data.MIMETYPE + " in (?, ?)" + " AND " +
ContactsContract.Data.HAS_PHONE_NUMBER + " = '" + 1 + "'"
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
val sortOrder = ContactsContract.Contacts.SORT_KEY_ALTERNATIVE
val uri = ContactsContract.CommonDataKinds.Contactables.CONTENT_URI
val cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
val mimeTypeIdx = cursor!!.getColumnIndex(ContactsContract.Contacts.Data.MIMETYPE)
val idIdx = cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID)
val nameIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
val dataIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.DATA)
val typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.TYPE)
val photo = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.PHOTO_URI)
val account_type = cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)
while (cursor.moveToNext()) {
var mListMobile = ArrayList<MobileType>()
val id = cursor.getLong(idIdx)
val type = cursor.getInt(typeIdx)
val number = cursor.getString(dataIdx)
val mimeType = cursor.getString(mimeTypeIdx)
val mNameContact = cursor.getString(nameIdx)
val photo_uri = cursor.getString(photo)
val account_t = cursor.getString(account_type)
when (type) {
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, 0 -> {
Log.i(TAG, ""+number)
}
ContactsContract.CommonDataKinds.Phone.TYPE_HOME -> {
Log.i(TAG, ""+number)
}
ContactsContract.CommonDataKinds.Phone.TYPE_WORK -> {
Log.i(TAG, ""+number)
}
}
}
cursor.close()