最好的事情是它会为您提供所有带有国家代码附加的唯一联系人
对于 kotlin,您可以像下面这样使用它
fun fetchContacts(context: Context): Set<ContactModel> {
val list = HashSet<ContactModel>()
val startnow: Long = android.os.SystemClock.uptimeMillis()
val util = PhoneNumberUtil.getInstance()
val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val countryCodeValue = tm.networkCountryIso.toUpperCase()
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.Contacts.IN_VISIBLE_GROUP + " = '" + 1 + "' 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
var uri: Uri? = null
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
uri = ContactsContract.CommonDataKinds.Contactables.CONTENT_URI
} else {
uri = ContactsContract.Data.CONTENT_URI
}
// we could also use Uri uri = ContactsContract.Data.CONTENT_URI;
// we could also use Uri uri = ContactsContract.Contact.CONTENT_URI;
val cursor = context.contentResolver.query(uri!!, projection, selection, selectionArgs, sortOrder)
val mimeTypeIdx = cursor!!.getColumnIndex(ContactsContract.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 photo = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.PHOTO_URI)
val typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.TYPE)
val account_type = cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)
while (cursor.moveToNext()) {
val contact_id = cursor.getInt(idIdx)
val photo_uri = cursor.getString(photo)
val contact_name = cursor.getString(nameIdx)
val contact_acc_type = cursor.getString(account_type)
val contact_type = cursor.getInt(typeIdx)
val contact_data = cursor.getString(dataIdx)
val Contact_mimeType = cursor.getString(mimeTypeIdx)
if (Contact_mimeType == ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) {
try {
val number = util.parse(contact_data, countryCodeValue)
val model = ContactModel()
model.id = contact_id.toString()
model.name = contact_name
model.mobileNumber = "${number.countryCode}${number.nationalNumber}"
// model.mobileNumber = contact_data
model.photoURI = photo_uri
list.add(model)
// Log.e("Number", contact_data)
} catch (e: Exception) {
showlog(TAG, Log.getStackTraceString(e))
}
}
}
cursor.close()
val endnow: Long = android.os.SystemClock.uptimeMillis()
showlog(TAG, "TimeForContacts " + (endnow - startnow) + " ms of size " + list.size)
return list
}
在这个google phonelibnumber中使用
实施 'com.googlecode.libphonenumber:libphonenumber:8.9.5'
Pojo 或数据类
data class ContactModel(
var id: String = "",
var name: String = "",
var mobileNumber: String = "",
var photo: Bitmap? = null,
var photoURI: String? = "",
var showContactInitial: Boolean = false,
var connected: Boolean = false,
var userid: String = "",
var request_status: String = "",
var hasBtwixt: Boolean = false,
var createdby: String = "",
var image: String = "",
var request_id:String = ""
)