gpt4 book ai didi

android - 安装 whatsapp 时获取联系人会提供重复的条目

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:42 27 4
gpt4 key购买 nike

我正在从 android 获取联系人并将其存储在数据库中。在模拟器上测试它并且它正在工作。但是当我在手机上测试该应用程序时,我得到了使用 Whatsapp 的联系人的重复条目。下面是代码 fragment 。

class LoadContactAsync extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {
Cursor people = getContentResolver()
.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
+ ") ASC");
while (people.moveToNext()) {
final String contactId = people.getString(people
.getColumnIndex(ContactsContract.Contacts._ID));
final String contactName = people
.getString(people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
final String image_uri = people
.getString(people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
final String hasPhone = people
.getString(people
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ((Integer.parseInt(hasPhone) > 0)) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
while (phones.moveToNext()) {
final String phoneNumber = phones
.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
db.addContact(new Contact(contactName, phoneNumber, image_uri));
}
phones.close();
}
}
people.close();
return null;
}

@Override
protected void onPostExecute(String s) {
pd.dismiss();
showToast("Contacts Added Successfully");
db.close();
}

@Override
protected void onPreExecute() {
pd = ProgressDialog.show(MainActivity.this, "Please Wait...",
"Fetching One Time Contacts", true, false);
}
}

这是截图

没有安装 Whatsapp

With No Whatsapp installed

安装了 Whatsapp

With Whatsapp Installed

任何帮助将不胜感激。谢谢

最佳答案

如果您不想使用数据库来避免重复条目,请使用以下代码

    private HashMap<String, ContactModel> getContactsList() {
String[] projection = new String[]{ContactsContract.Contacts._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.PHOTO_URI};
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");


String lastPhoneName = " ";
if (phones.getCount() > 0) {
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactId = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
String photoUri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (!name.equalsIgnoreCase(lastPhoneName)) {
lastPhoneName = name;
contactDetail.put(contactId, new ContactModel(contactId, name, phoneNumber, photoUri));
Log.d("getContactsList", name + "---" + phoneNumber + " -- " + contactId + " -- " + photoUri);
}
}
}
phones.close();

return contactDetail;
}

关于android - 安装 whatsapp 时获取联系人会提供重复的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35427317/

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