Android 联系人选择器获取姓名 + 号码 + 电子邮件

Posted

技术标签:

【中文标题】Android 联系人选择器获取姓名 + 号码 + 电子邮件【英文标题】:Android Contact Picker get Name + Number + Email 【发布时间】:2016-01-02 10:51:54 【问题描述】:

我在使用联系人选择器时遇到问题,它适用于 Phone.DISPLAY_NAME 和 Phone.NUMBER,但不适用于 Email.ADDRESS

public class CustomerForm extends Activity 
private final static int CONTACT_PICKER = 1;
private EditText txtMailContacto;
private EditText txtNombreContacto;
private EditText txtTelefono;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer_form);
    txtMailContacto = (EditText) findViewById(R.id.txtMailContacto);
    txtTelefono = (EditText) findViewById(R.id.txtTelefono);
    txtNombreContacto = (EditText) findViewById(R.id.txtNombreContacto);

public void pickContact(View v)

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    // check whether the result is ok
    if (resultCode == RESULT_OK) 
        // Check for the request code, we might be usign multiple startActivityForReslut
        switch (requestCode) 
        case CONTACT_PICKER:
            contactPicked(data);
            break;
        
     else 
        Log.e("MainActivity", "Failed to pick contact");
    

private void contactPicked(Intent data) 
    Cursor cursor = null;
    try 
        String mail = null ;
        String phoneNo = null ;
        String name = null;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the email
        int  mailIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
        // column index of the contact name
        int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        mail = cursor.getString(mailIndex);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        // Set the value to the textviews
        txtMailContacto.setText(mail);
        txtTelefono.setText(phoneNo);
        txtNombreContacto.setText(name);
     catch (Exception e) 
        e.printStackTrace();
    


所以,我正在尝试获取这些字段,但我无法弄清楚问题所在。 感谢您的帮助...

【问题讨论】:

【参考方案1】:

知道了!!!

public class CustomerForm extends Activity 
private final static int CONTACT_PICKER = 1;
private EditText txtMailContacto;
private EditText txtNombreContacto;
private EditText txtTelefono;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer_form);
    txtMailContacto = (EditText) findViewById(R.id.txtMailContacto);
    txtTelefono = (EditText) findViewById(R.id.txtTelefono);
    txtNombreContacto = (EditText) findViewById(R.id.txtNombreContacto);

public void pickContact(View v)

    Intent contactPickerIntent = 
            new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        // check whether the result is ok
    if (resultCode == RESULT_OK) 
        // Check for the request code, we might be using multiple startActivityForReslut
        switch (requestCode) 
        case CONTACT_PICKER:
            contactPicked(data);
            break;
        
     else 
        Log.e("MainActivity", "Failed to pick contact");
    

private void contactPicked(Intent data) 
    ContentResolver cr = getContentResolver();
    try 
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cur = cr.query(uri, null, null, null, null);
        cur.moveToFirst();
                                                // column index of the contact ID
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                                                // column index of the contact name 
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        txtNombreContacto.setText(name);        //print data            
                                                // column index of the phone number
        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                    new String[]id, null);
                    while (pCur.moveToNext()) 
                        String phone = pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        txtTelefono.setText(phone);         //print data
                     
                    pCur.close();
                                                // column index of the email   
        Cursor emailCur = cr.query(        
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",  
                new String[]id, null); 
            while (emailCur.moveToNext())  
                // This would allow you get several email addresses
                    // if the email addresses were stored in an array  
                String email = emailCur.getString(
                              emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));                                         
                txtMailContacto.setText(email);         //print data
         
        emailCur.close();
     catch (Exception e) 
        e.printStackTrace();
    


【讨论】:

你的第一个查询好像没用? Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); @behelit 同意!我在没有初始查询的情况下对其进行了测试,它工作正常。编辑了答案并将其删除。 现在你已经删除了Cursor cur,代码不完整【参考方案2】:

有史以来最好的答案,它对你有很大帮助 只需选择联系人姓名或联系电话,工作完美,请在使用前授予联系人阅读权限。

@Override
public void onActivityResult(int RequestCode, int ResultCode, Intent ResultIntent) 

    super.onActivityResult(RequestCode, ResultCode, ResultIntent);

    switch (RequestCode) 

        case (7):
            if (ResultCode == Activity.RESULT_OK) 

                Uri uri;
                Cursor cursor1, cursor2;
                String TempNameHolder, TempNumberHolder, TempContactID, IDresult = "" ;
                int IDresultHolder ;

                uri = ResultIntent.getData();

                cursor1 = getContentResolver().query(uri, null, null, null, null);

                if (cursor1.moveToFirst()) 

                    TempNameHolder = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    TempContactID = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID));

                    IDresult = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    IDresultHolder = Integer.valueOf(IDresult) ;

                    if (IDresultHolder == 1) 

                        cursor2 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + TempContactID, null, null);

                        while (cursor2.moveToNext()) 

                            TempNumberHolder = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            name.setText(TempNameHolder);

                            number.setText(TempNumberHolder);

                        
                    

                    
                
                break;
            
    

【讨论】:

以上是关于Android 联系人选择器获取姓名 + 号码 + 电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

从联系人选择器意图中选择电子邮件、姓名和电话号码

从联系人选择器中获取号码

ios中的联系人选择器以获取电话号码

如何从android中的联系人姓名获取联系人的号码

如何从联系人列表中获取姓名

如何将联系人姓名、号码、contact_id 保存到 ArrayList / 联系人选择器