![](/img/trans.png)
[英]How to get contact details from contact number in android programatically
[英]How to programatically delete a native contact using the contact number in android?
我需要僅使用 MSISDN 從本機聯系人列表中刪除聯系人。 目前,我正在查看所有聯系人並在號碼上進行匹配以獲取名稱。 有沒有更有效的方法來做到這一點?
這是我的代碼:
獲取所有本地聯系人:
override fun nativeContactList(): Single<List<NativeContact>> {
val contacts = ArrayList<NativeContact>()
val cursor = context.contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null,
null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC")
val normalizedNumbersAlreadyFound = HashSet<String>();
val indexOfNormalizedNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER)
val indexOfDisplayName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
val indexOfDisplayNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
cursor.use { cursor ->
while (cursor.moveToNext()) {
val normalizedNumber = cursor.getString(indexOfNormalizedNumber)
if (normalizedNumbersAlreadyFound.add(normalizedNumber)) {
val displayName = cursor.getString(indexOfDisplayName)
val displayNumber = cursor.getString(indexOfDisplayNumber)
Timber.d("Existing contact: $displayName $displayNumber")
contacts.add(NativeContact(displayName, displayNumber, null, null))
}
}
cursor.close()
}
return Single.fromCallable {
contacts
}
}
獲得本地聯系人列表后,我會遍歷列表並查找匹配項以獲得名稱:
Observable.fromIterable(nativeContacts)
.subscribeOn(processScheduler)
.subscribe(object : DisposableObserver<NativeContact>() {
override fun onComplete() {
Timber.d("Finished going thorough contacts")
}
override fun onError(e: Throwable) {
Timber.d("Warning: exception occurred while looping through native contacts list")
}
override fun onNext(nativeContact: NativeContact) {
if (contactToDelete.number == nativeContact.phoneNumber) {
contactToDelete.firstName = nativeContact.name
deprovisionContact(contactToDelete)
}
}
})
在我有了姓名和號碼后,我刪除了聯系人:
public Completable deleteContact(final ContactToDelete contactToDelete) {
return Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
Timber.d("Attempting to delete " + contactToDelete.getNumber() + " " + contactToDelete.getName());
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactToDelete.getNumber()));
Cursor cursor = context.getContentResolver().query(contactUri, null, null, null, null);
try {
if (cursor.moveToFirst()) {
do {
String name = contactToDelete.getName();
if (cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(name)) {
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
context.getContentResolver().delete(uri, null, null);
Timber.d("deleted " + contactToDelete.getNumber() + " " + contactToDelete.getName());
}
} while (cursor.moveToNext());
}
} catch (Exception e) {
Timber.e("Error while attempting to delete contact " + e.getMessage());
} finally {
cursor.close();
}
}
});
}
有沒有更有效的方法來做到這一點? 即每次我們要刪除聯系人時不要循環訪問聯系人列表? 先感謝您。
我在這里發現了一些問題。
首先您的要求不清楚,您是要刪除包含此電話號碼的所有聯系人嗎? 電話號碼不是唯一標識符,可以由多個聯系人共享,您應該刪除所有這些聯系人嗎?
你遍歷所有聯系人只是為了得到名字? 為什么你需要一個名字來刪除一個聯系人,這也是一個非唯一的標識符,可能有多個聯系人具有相同的名字和相同的號碼。
你在這里做了太多的步驟,我看到你已經熟悉 PHONE_LOOKUP 表,你只需要使用它來刪除具有請求電話號碼的聯系人。
代碼示例(未測試):
// get all contact-ids of all deletion candidates
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String[] projection = new String[]{ PhoneLookup.CONTACT_ID };
Cursor idsCursor = resolver.query(uri, projection, null, null, null);
// iterate the cursor and delete all the contact-ids one-by-one
while (idsCursor != null && idsCursor.moveToNext()) {
Long contactId = idsCursor.getLong(0);
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
context.getContentResolver().delete(contactUri, null, null);
}
if (idsCursor != null) {
idsCursor.close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.