gpt4 book ai didi

android - 联系人提取需要太多时间

转载 作者:行者123 更新时间:2023-11-29 19:21:57 26 4
gpt4 key购买 nike

在我的代码中,获取联系人需要花费太多时间来获取联系人并在应用程序中显示。请指导我哪里错了,我应该更正什么以加快执行时间。

这是我的代码。

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null );
String phone = null;
List<String> phonenumber = new ArrayList<String>();
String emailContact = null;
String emailType = null;
String image_uri = "";
Bitmap bitmap = null;
if (cur.getCount() > 0) {
while (cur.moveToNext())
{
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
System.out.println("name : " + name + ", ID : " + id);

Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[]{id}, null);
Log.e("pCur","dfgfdg "+pCur.getCount());
while (pCur.moveToNext())
{
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// contactid=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

/* phonenumber.add(pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));`*/
Log.e("phone" ,phone);

}
pCur.close();


Cursor emailCur = cr.query
(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[]{id}, null);

while (emailCur.moveToNext())
{
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

if(TextUtils.isEmpty(emailContact)||emailContact.equalsIgnoreCase(null)||emailContact.equalsIgnoreCase(""))
{
emailContact="";

Log.e("isEmpty","isEmpty " + emailContact);
}

else
{
Log.e("gfdszfg","Email " + emailContact);
}
/* emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));*/

Log.e("gfdszfg","Email " + emailContact);
}
emailCur.close();
}

if (image_uri != null)
{
System.out.println(Uri.parse(image_uri));
try
{
bitmap = MediaStore.Images.Media
.getBitmap(this.getContentResolver(),
Uri.parse(image_uri));
System.out.println(bitmap);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

mList.add(new Contacts(name, phone, image_uri,emailContact));
emailContact="";
}
cur.close();
mMyContactsAdapter = new MyContactsAdapter(MainActivity.this, mList);
mcontact.setAdapter(mMyContactsAdapter);
}

在我的代码中,我的联系人获取 while 循环循环了 3 次,我不知道为什么。

最佳答案

我遇到过你现在遇到的这种情况。试试下面的代码

public static ArrayList ReadContactsSpecialWay(Context context) {
StringBuffer contactBuffer = new StringBuffer();
StringBuffer emailBuffer = new StringBuffer();

ArrayList<String> contactList = new ArrayList<>();
ArrayList<String> emailList = new ArrayList<>();
ArrayList<ContactModel> contactModelsList = new ArrayList<>();
HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>();
String[] emailsAndContacts = new String[2];

// Phone numbers
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};

Cursor phoneNumbers = context.getContentResolver().query(uri, projection, null, null, null);
int indexContactID = phoneNumbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
int indexName = phoneNumbers.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int indexNumber = phoneNumbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
while (phoneNumbers.moveToNext()) {
Integer contactId = phoneNumbers.getInt(indexContactID);
String contactName = phoneNumbers.getString(indexName);
String contactNumber = phoneNumbers.getString(indexNumber).replace(" ", "");

ContactModel contactModel = new ContactModel();
contactModel.setContactPhone(contactNumber);
contactNumber = contactModel.getContactPhone();

if (hashMap.containsKey(contactId))
contactModel.setContactName(contactName + " (" + contactNumber + ")");
else {
hashMap.put(contactId, true);
contactModel.setContactName(contactName);
}
contactModelsList.add(contactModel);
contactList.add(contactNumber);
LogHelper.informationLog(contactNumber + " " + contactName);
contactBuffer.append(contactNumber + ",");
}
phoneNumbers.close();

// Emails
uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
projection = new String[]{
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
Cursor emails = context.getContentResolver().query(uri, projection, null, null, null);
indexContactID = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID);
indexName = emails.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int indexData = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
while (emails.moveToNext()) {
Integer contactId = emails.getInt(indexContactID);
String contactName = emails.getString(indexName);
String contactEmail = emails.getString(indexData).toLowerCase();
ContactModel contactModel = new ContactModel();
contactModel.setEmailAddress(contactEmail);

if (hashMap.containsKey(contactId)) {
contactModel.setEmailName(contactName + " (" + contactEmail + ")");
} else {
contactModel.setEmailName(contactName);
hashMap.put(contactId, true);
}
contactModelsList.add(contactModel);

if (contactEmail != null && !emailList.contains(contactEmail)) {
emailList.add(contactEmail);
LogHelper.informationLog(contactEmail + " " + contactName);
emailBuffer.append(contactEmail + ",");
}
}
emails.close();

if (contactBuffer.toString().length() > 0) {
emailsAndContacts[0] = contactBuffer.toString().substring(0, (contactBuffer.toString().length() - 1));
} else
emailsAndContacts[0] = "";

if (emailBuffer.toString().length() > 0) {
emailsAndContacts[1] = emailBuffer.toString();
} else {
emailsAndContacts[1] = "";
}

if(contactModelsList.size() == 0)
return null;
else {
ArrayList specialList = new ArrayList();
specialList.add(contactModelsList);
specialList.add(emailsAndContacts);

return specialList;
}
}

它一定会帮助你。

关于android - 联系人提取需要太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42250015/

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