gpt4 book ai didi

android - 无法读取 MIUI 中的联系人

转载 作者:行者123 更新时间:2023-11-30 05:10:38 33 4
gpt4 key购买 nike

我写了这个函数来检索 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;
}

这段代码在除小米手机外的所有安卓设备上都可以正常工作。我认为这可能是 MIUI 设备的问题。

当我在 MIUI ROM 上运行时,此函数返回空 HashMap 。

最佳答案

最好的事情是它会给你所有唯一的联系方式,附加国家代码

对于 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 中使用

implementation '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 = ""
)

关于android - 无法读取 MIUI 中的联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53812751/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com