Android 联系人选择器 : pick phone or email

标签 android email android-contacts phone-number picker

Android 联系人选择器是否可以显示电话号码和电子邮件地址列表,并且用户可以选择电话或电子邮件。

我尝试了以下 Intent :

Intent.ACTION_PICK, Contacts.CONTENT_URI // - selects the entire contact
Intent.ACTION_PICK, Phone.CONTENT_URI    // - select only phones
Intent.ACTION_PICK, Email.CONTENT_URI    // - select only emails

最佳答案

已经得到答复:Link to answer 但我将在这里发布代码

private void contactPicked(Intent data) {
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    cur.moveToFirst();
    try {
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cur = getContentResolver().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();
    }
}
}

关于Android 联系人选择器 : pick phone or email,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37957689/

相关文章:

python - Repl.it SMTPLIB OSError : [Errno 99] Cannot assign requested address

android - 如何通过一次查询搜索地址为 (FORMATTED_ADDRESS) 的联系人?

android - Honeycomb/ICS 中的地址格式

android - 从 Android 设备获取超过 10000 个联系人

android - 在不使用 FLAG 的情况下清除 Activity 中的 Backstack

Android 如何在 PreferenceScreen 长时间操作期间禁用复选框

c# - 发送到短信电子邮件地址的多个 sendgrid 电子邮件出现故障

javascript - 如何使用 Javascript(不使用 JQuery)进行电子邮件地址验证?

android - 链接器找不到函数

android - 在 Android 中重置 INTEGER PRIMARY KEY AUTOINCREMENT