如何更快地阅读联系人?

时间:2018-01-25 12:53:46

标签: java android android-contacts

我尝试制作Contact App,现在我尝试阅读联系人 我有450个联系人,它需要60秒才能全部阅读。

我使用Contact class:

public class Contact {
String name;
public ArrayList<String> PhoneNumber =  new ArrayList<>();
public ArrayList<String> Email = new ArrayList<>();
int NumberOfPhones = 0;
int NumberOfMails = 0 ;
}

我读的是这样的:

tatic final int CODE_FOR_PERMISSION = 123;
List<Contact> ListContact = new ArrayList<Contact>();
Contact TempContact;
String TempName ="";
ArrayList<String> TempPhoneNumber = new ArrayList<>();
public ArrayList<String> TempEmail = new ArrayList<>();
int TempCounter = 0;
private ProgressDialog pDialog;
private Handler updateBarHandler;
ArrayList<String> contactList;
Cursor cursor;
int counter;
TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_activity);
    AskForPermission();

    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Reading contacts...");
    pDialog.setCancelable(false);
    pDialog.show();

    updateBarHandler = new Handler();

    // Since reading contacts takes more time, let's run it on a separate thread.
    new Thread(new Runnable() {

        @Override
        public void run() {
            getContacts();
        }
    }).start();
    //init();
    //OnAction();




void init(){
    textView = (TextView) findViewById(R.id.chekk);


}

void OnAction(){

  TempContact.PrintToTextView(textView,ListContact);




}




public void getContacts() {

    contactList = new ArrayList<String>();

    String phoneNumber = null;
    String email = null;

    Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
    String _ID = ContactsContract.Contacts._ID;
    String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
    String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

    Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
    String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

    Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
    String DATA = ContactsContract.CommonDataKinds.Email.DATA;

    StringBuffer output;

    ContentResolver contentResolver = getContentResolver();

    cursor = contentResolver.query(CONTENT_URI, null, null, null, null);

    // Iterate every contact in the phone
    if (cursor.getCount() > 0) {

        counter = 0;
        while (cursor.moveToNext()) {

            output = new StringBuffer();

            // Update the progress message
            updateBarHandler.post(new Runnable() {
                public void run() {
                    pDialog.setMessage("Reading contacts : " + counter++ + "/" + cursor.getCount());
                }
            });

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

            int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));

            if (hasPhoneNumber > 0) {

                output.append("\n First Name:" + name);
                TempName = name;

                //This is to read multiple phone numbers associated with the same contact
                Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null);

                while (phoneCursor.moveToNext()) {
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    output.append("\n Phone number:" + phoneNumber);
                    TempPhoneNumber.add(phoneNumber);
                }

                phoneCursor.close();

                // Read every email id associated with the contact
                Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID + " = ?", new String[]{contact_id}, null);

                while (emailCursor.moveToNext()) {

                    email = emailCursor.getString(emailCursor.getColumnIndex(DATA));
                    TempEmail.add(email);
                    output.append("\n Email:" + email);

                }

                emailCursor.close();

                String columns[] = {
                        ContactsContract.CommonDataKinds.Event.START_DATE,
                        ContactsContract.CommonDataKinds.Event.TYPE,
                        ContactsContract.CommonDataKinds.Event.MIMETYPE,
                };

                String where = ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
                        " and " + ContactsContract.CommonDataKinds.Event.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE + "' and " + ContactsContract.Data.CONTACT_ID + " = " + contact_id;

                String[] selectionArgs = null;
                String sortOrder = ContactsContract.Contacts.DISPLAY_NAME;

                Cursor birthdayCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, columns, where, selectionArgs, sortOrder);
                Log.d("BDAY", birthdayCur.getCount()+"");
                if (birthdayCur.getCount() > 0) {
                    while (birthdayCur.moveToNext()) {
                        String birthday = birthdayCur.getString(birthdayCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
                        output.append("Birthday :" + birthday);
                        Log.d("BDAY", birthday);
                    }
                }
                birthdayCur.close();
            }

            // Add the contact to the ArrayList
            contactList.add(output.toString());
            TempContact = new Contact(TempName,TempPhoneNumber,TempEmail);
            ListContact.add(TempContact);
            TempName = "";
            TempPhoneNumber.clear();
            TempEmail.clear();
        }



        // Dismiss the progressbar after 500 millisecondds
        updateBarHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                pDialog.cancel();
            }
        }, 5);
    }

}

如何让联系人更快地阅读? 我搜索过其他的东西,但我不知道怎么做得更快。 它可以完成,因为手机中的默认联系人应用程序非常快。

1 个答案:

答案 0 :(得分:0)

您无需查询所有联系人+所有电子邮件+所有手机即可显示与默认联系人应用类似的主要联系人列表。 您可能会注意到所有联系人应用程序的主屏幕仅显示姓名+图片,而不显示电子邮件或电话。 只需在代码中执行第一个查询即可显示联系人列表,并且只有当用户点击其中一个联系人时,您才会打开一个新活动,其中只包含该特定联系人的详细信息(这是一个简短的查询) )