每当单击 Tab 时,我都会从手机加载联系人列表。第一次触摸它工作正常,但第二次会导致内存问题。我应该如何防止这个问题?
这是代码。
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
try {
contactsCursor= getContentResolver()
.query(uri, null, null, null, sortOrder);
//Log.i("The first one", "" + contactsCursor.getCount());
if(contactsCursor.getCount()>0){
if(contactsCursor.moveToFirst()){
while (contactsCursor.moveToNext()) {
String hasPhoneNumber = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
int contTypeInt=0;
String contactType="";
ArrayList<String> phoneNumberList= new ArrayList<String>();
if (Integer.parseInt(hasPhoneNumber) > 0) {
String id = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
String phoneNumber = null;
if(phones!=null && phones.getCount()>0){
while (phones.moveToNext()) {
int type= phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if (type == Phone.TYPE_MOBILE){
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumberList.add(phoneNumber);
}
}
contTypeInt++;
phones.close();
phones = null;
}
}
String id = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + id, null, null);
ArrayList<String> emailAddressList= new ArrayList<String>();
if(emails!=null && emails.getCount()>0){
while (emails.moveToNext())
{
// This would allow you get several email addresses
String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(strEmails.equals("")){
strEmails= emailAddress;
}else{
strEmails= strEmails+","+emailAddress;
}
Log.v(name+"==>", emailAddress);
if ((!emailAddress.equalsIgnoreCase(""))&&(emailAddress.contains("@")))
{
Log.d("email", emailAddress);
emailAddressList.add(emailAddress);
}
}
emails.close();
emails = null;
}
if(emailAddressList.size()>0){
//primaryEmailList.add(emailAddressList.get(0));
contTypeInt++;
}
if(contTypeInt==0){
contactType="";
}else if(contTypeInt==2){
contactType= "both";
}else if(contTypeInt==1){
if(emailAddressList.size()>0){
contactType="email";
}else{
contactType="phone";
}
}
Log.d(name+" conatctType", contactType);
if(name==null){
name="";
}
String []tempArr= name.split(" ");
for(int i=0;i<tempArr.length;i++){
if(tempArr[i].length()>1){
tempArr[i]= tempArr[i].substring(0, 1).toUpperCase() + tempArr[i].substring(1);
}
}
String nameTmp="";
for(int i=0;i<tempArr.length;i++){
if(nameTmp.equals("")){
nameTmp= tempArr[i];
}else{
nameTmp= nameTmp+" "+tempArr[i];
}
}
Constants.contactsList.add(new ContactsData(id, nameTmp, contactType, phoneNumberList, emailAddressList));
phoneNumberList = null;
emailAddressList = null;
tempArr = null;
}
}
}
contactsCursor.close();
contactsCursor = null;
}
catch (Exception e) {
}
有没有人可以帮助我?提前致谢