【问题标题】:Android contact picker: pick phone or emailAndroid 联系人选择器:选择电话或电子邮件
【发布时间】:2016-10-23 18:50:22
【问题描述】:

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

我尝试了以下 Intents:

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

【问题讨论】:

    标签: android email android-contacts phone-number picker


    【解决方案1】:

    已经回答了: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();
        }
    }
    }
    

    【讨论】:

    • 我试图做的是配置联系人选择器以呈现允许选择电子邮件或电话号码的 UI。看来这是不可能的。我确实使用了上面的代码并创建了一个 UI 来显示从 Android 联系人选择器中选择的联系人的电子邮件和电话列表。
    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 2012-05-14
    • 2016-01-02
    • 2012-06-13
    • 2011-04-28
    • 2019-06-03
    • 1970-01-01
    相关资源
    最近更新 更多