android获取联系人详细信息,如数字和缩略图

时间:2015-04-01 19:41:26

标签: android performance thumbnails android-contentprovider android-contacts

我使用此代码可以获取联系人的号码和联系人的缩略图,但这不适合加载和准备联系人。

请帮助我提高此代码的效果:

List<Contact_Pojo> list = new ArrayList<Contact_Pojo>();
ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        null, null, Phone.DISPLAY_NAME + " ASC");

int i = 0;
if (cur.moveToFirst()) {
    while (cur.moveToNext()) {

        Cursor phoneCursor = getActivity()
                .getContentResolver()
                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, },
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?",
                        new String[] { cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts._ID)) },
                        null);

        if (phoneCursor.moveToFirst()) {
            Contact_Pojo personContact = new Contact_Pojo();
            /*
             * Id
             */
            personContact.setId(cur.getString(1));
            /*
             * Name
             */
            personContact
                    .setName(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
            /*
             * Photo ID
             */
            personContact
                    .setImageUrl(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)));
            /*
             * Number
             */
            personContact.setNumber(phoneCursor.getString(0));
            //
            list.add(personContact);
        }

        phoneCursor.close();

    }
}

cur.close();
return list;

2015年4月4日更新

  • 它很慢,因为它从联系人提供者那里得到了所有列,在这个表中有+20列需要花费很多时间。

3 个答案:

答案 0 :(得分:3)

如何使用AsyncTaskLoader,

当您想要获取联系人列表时编写此代码

getLoaderManager() or getSupportLoaderManager().initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> loadingCallback)

public class LoadingCallback implements LoaderManager.LoaderCallbacks<Void> {
  @Override
  public Loader<Void> onCreateLoader(int i, Bundle bundle) {
    // Show your dialog;
    return new InitializeContactsTask(context, (ArrayList) contactList, mAdapter);
  }

  @Override
  public void onLoadFinished(Loader<Void> voidLoader, Void aVoid) {
    // UI Work here after background task and hide dialog.
  }

  @Override
  public void onLoaderReset(Loader<Void> voidLoader) {
  }
}

public class InitializeApplicationsTask extends AsyncTaskLoader<Void> {
  @Override
  protected void onStartLoading() {
      forceLoad();
  }

  @Override
  public Void loadInBackground() {
      // Query the contacts here and return null

      return null;
  }
}

答案 1 :(得分:1)

它很慢,因为您从联系提供程序获取所有列,因此使用projection只获取所需的列。像

String[] projection = {
ContactsContract.Contacts.DISPLAY_NAME // this is just example, add fields that required } ;

请告诉我第二次查询的目的是什么?因为这是我联系的结果

enter image description here

和我获取所有联系的方法是

@SuppressLint("InlinedApi")
    public void readContacts() {

        long l1 = System.currentTimeMillis();

        Log.d("DEBUG",
                "starting readContacts() time " + l1);

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        if (cur.getCount() > 0) {

            while (cur.moveToNext()) {

                Contact_pojo cp = new Contact_pojo();

                String Contact_Id = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String Contact_Name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String photothumb = "";
                try {
                    photothumb = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                    if (photothumb != null
                            && !photothumb.toString().equalsIgnoreCase("")) {
                        cp.setImageUri(photothumb);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

                cp.setId(Contact_Id);

                cp.setName(Contact_Name);


Cursor phones = cr.query(Phone.CONTENT_URI, null,
                    Phone.CONTACT_ID + " = " + Contact_Id, null, null);
            while (phones.moveToNext()) {
                String number = phones.getString(phones
                        .getColumnIndex(Phone.NUMBER));
                int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));

                Log.d("DEBUG", "number " + number + " type " + type);
                cp.setNumber(number);

            }
            phones.close();
                contactList.add(cp);

            }
        }
        long l2 = System.currentTimeMillis();
        Log.d("DEBUG",
                "Finished readContacts() time " + l2);
        Log.d("DEBUG","Total contact loaded "+contactList.size()+" within "+ (l2 - l1) + "ms");

    }

答案 2 :(得分:0)

不要使用双重查询,如果设备有很多联系人并且它在某些设备上也无法正常工作,则需要花费太多时间......

您还可以通过单个查询获取联系人。

使用以下代码..

public void fetchContacts() 
    {

        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID};



        String _ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String DISPLAY_NAME =  Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

        Cursor cursor = mActivity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

        System.out.println("contact size.."+cursor.getCount());

        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

                String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
                String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
                String phoneNumber = cursor.getString(cursor.getColumnIndex(NUMBER));



                System.out.println(" name : "+name);
                System.out.println(" number : "+phoneNumber);

            }
        }
    }

检查以下链接以获取联系人照片。

ContactsContract.Contacts.Photo

Getting a Photo from a Contact