阅读联系人电话和电子邮件

Posted

技术标签:

【中文标题】阅读联系人电话和电子邮件【英文标题】:Read Contacts phone and email 【发布时间】:2017-01-09 08:39:34 【问题描述】:

我正在使用下面的代码来读取带有电话号码和电子邮件的联系人!它成功读取所有联系人,但如果您的联系人没有电子邮件,它会显示前一个联系人的电子邮件。

如下图!有任何想法吗?? 提前谢谢你

public void getContacts() 

    contactList = new ArrayList<Contacts>();
    String phoneNumber = null;
    String email = null;
    ContentResolver contentResolver = getContentResolver();


    String name = null;

    cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    // Iterate every contact in the phone
    if (cursor.getCount() > 0) 
        counter = 0;

        while (cursor.moveToNext()) 

            // 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(ContactsContract.Contacts._ID));
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


            int hasPhoneNumber = Integer.parseInt(cursor.getString(
                    cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
            if (hasPhoneNumber > 0) 

                //This is to read multiple phone numbers associated with the same contact
                Cursor phoneCursor = contentResolver.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]contact_id, null);

                while (phoneCursor.moveToNext()) 
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));
                
                phoneCursor.close();
            

            Cursor emailCursor = contentResolver.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID
                    + " = ?", new String[]contact_id, null);

            while (emailCursor.moveToNext()) 

                email = emailCursor.getString(emailCursor.getColumnIndex(
                        ContactsContract.CommonDataKinds.Email.DATA));
            

            emailCursor.close();
            contact = new Contacts();

            contact.setContactFName(name);
            contact.setContactPhone(phoneNumber);
            contact.setContactEmail(email);

            contactList.add(contact);
        

        // ListView has to be updated using a ui thread
        runOnUiThread(new Runnable() 
            @Override
            public void run() 
                adapter = new ImportContactAdapter
                        (ImportContactActivity.this, R.layout.custom_import_contact, contactList);
                mListView.setAdapter(adapter);
            
        );
        // Dismiss the progressbar after 500 millisecondds
        updateBarHandler.postDelayed(new Runnable() 
            @Override
            public void run() 
                pDialog.cancel();
            
        , 500);
    

enter image description here

【问题讨论】:

Accessing the Contact name, Number and Email ID?的可能重复 【参考方案1】:

这与变量的范围有关。您在 while(cursor.moveToNext()) 之外声明您的 String email。当您的联系人没有任何电子邮件时,while (emailCursor.moveToNext()) 不会更新您的 String email 的值,这意味着当您执行 contact.setContactEmail(email); 时,它仍然包含之前联系人的值。

【讨论】:

以上是关于阅读联系人电话和电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

分组表视图空白部分

从电话联系人中获取姓名、电话号码和电子邮件地址[关闭]

从 Android 联系人中获取电子邮件和电话

我无法从电话联系人中加载电子邮件地址

Swift 3 添加带有电话和电子邮件信息的新联系人

Android 联系人选择器:选择电话或电子邮件