Hi am currently doing a project related to Contact ,Where I am fetching details from the contact*(Email,Number and Contactname)* and it does well.But the problem is it take a long time to fetch the contact details ( 1000+ contact including contact that sync from social network sites).so that I put a Asynchronous Task
for this purpose ,and it does well but the problem is due to a long time to complete the fetching process ,when I press the back button it crash during the Asynchronous task .My problem does not crash why this fetching contact take a lot of time.Is there way to get the contact
faster. My code to get contact detail is as given below
public void readContact() {
contactname = new ArrayList<String>();
contactnumber = new ArrayList<String>();
companyname_one = new ArrayList<String>();
contactemail = new ArrayList<String>();
people = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
PhoneLookup.DISPLAY_NAME);
while (people.moveToNext()) {
int nameFieldColumnIndex = people
.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
if (contact == null) {
contactname.add("No contact Set");
} else {
contactname.add(contact);
}
String szId = people.getString(people
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
cursor_one = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "='"
+ szId + "'", null, null);
if (cursor_one.moveToNext()) {
String number = cursor_one.getString(cursor_one
.getColumnIndex(Phone.NUMBER));
contactnumber.add(number);
cursor_one.close();
} else {
contactnumber.add("no number");
cursor_one.close();
}
emails_value = getContentResolver().query(
Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "='"
+ szId + "'", null, null);
if (emails_value.moveToNext()) {
email_sorting = emails_value.getString(emails_value
.getColumnIndex(Email.DATA));
checkAll();
} else {
contactemail.add("no email");
emails_value.close();
}
}
people.close();
System.out.println("noz " + contactnumber);
System.out.println("name" + contactname);
System.out.println("email" + contactemail);
System.out.println("noz size " + contactnumber.size());
System.out.println("name size " + contactname.size());
System.out.println("contactemail size " + contactemail.size());
}
The checkAll()
method is patter matching of email as below
public boolean checkAll() {
boolean chkAll = true;
Pattern p1 = Pattern.compile(".+@.+\\.[a-z]+");
Matcher m1 = p1.matcher(email_sorting.trim());
if (!m1.matches()) {
contactemail.add("no email");
contactemail_sort.add("no email");
emails_value.close();
chkAll = false;
} else {
contactemail.add(email_sorting);
contactemail_sort.add(email_sorting);
emails_value.close();
chkAll = true;
}
return chkAll;
}