我编写了此函数来检索Android手机中的联系人:
public static HashMap<String, Contact> getAllContacts(Context context) {
HashMap<String, Contact> contactsList = new HashMap<>();
if(!contactsReadPermission(context))
return contactsList;
DateTime timestamp = DateTime.now();
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.SORT_KEY_PRIMARY + " ASC");
if (cursor != null) {
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
if (phoneNumber == null)
continue;
contactsList.put(phoneNumber, new Contact(name, phoneNumber, timestamp.toString()));
}
phoneCursor.close();
}
}
cursor.close();
}
return contactsList;
}
并且此代码在所有Android设备上都可以正常工作,除了Xiaomi Phones。我认为这可能是MIUI设备的问题。
当我在MIUI ROM上运行该函数时,该函数将返回空的哈希映射。
答案 0 :(得分:0)
最好的是,它将为您提供带有国家/地区代码附加的唯一联系人
对于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 phonelib号
实现'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 = ""
)
答案 1 :(得分:0)
很抱歉浪费您的时间,以防您花时间在此上。我在将这段代码与SyncAdapter一起使用时,真正的问题完全是另外一些问题,实际上,除非我们在AppInfo中执行autostart = true,否则MIUI中的SyncAdapter才起作用。