1

我想在使用手机的默认联系人时在 android 中获取所选联系人的联系人姓名。我试过这样,但它显示了手机中的所有联系人姓名,但我只想要选择的联系人姓名

Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phNumber));

                    // query time
                    Cursor cursor = this.getContentResolver().query(contactUri, projection, null, null, null);

                    if(cursor != null) {
                        if (cursor.moveToFirst()) {
                            name =      cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                            Log.v("sai", "Started uploadcontactphoto: Contact Found @ " + phNumber);            
                            Log.v("sai", "Started uploadcontactphoto: Contact name  = " + name);
                        } else {
                            Log.v("sai", "Contact Not Found @ " + phNumber);
                        }
                        cursor.close();
                    }
4

2 回答 2

0

因此,您使用了正确的方法,但您并没有告诉 Android 要查找的具体联系人。

您没有说明“选定联系人”是什么,但我认为您应该知道它的电话号码或其他实际查询联系人列表的电话号码。

根据文档,您只是没有使用选择参数。

您可以像编写 SQL 查询的 WHERE 语句一样编写选择参数。如 ContactsContract.PhoneLookup + " = 123456789"

有关更详细的说明,请参阅此答案。

于 2013-11-12T07:50:50.503 回答
0

这是我在arraylist中存储电子邮件地址和联系人姓名的代码

public ArrayList<ContactVo> getNameEmailDetails() {
        ArrayList<ContactVo> m_arrList = new ArrayList<ContactVo>();
        ArrayList<String> emlRecs = new ArrayList<String>();
        HashSet<String> emlRecsHS = new HashSet<String>();
        ContentResolver cr = m_context.getContentResolver();
        String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.PHOTO_ID,
                ContactsContract.CommonDataKinds.Email.DATA,
                ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
        String order = "CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
                + " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
                + ContactsContract.Contacts.DISPLAY_NAME + ", "
                + ContactsContract.CommonDataKinds.Email.DATA
                + " COLLATE NOCASE";
        String filter = ContactsContract.CommonDataKinds.Email.DATA
                + " NOT LIKE ''";
        Cursor cur = cr.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION,
                filter, null, order);
        ContactVo m_vo;
        if (cur.moveToFirst()) {
            do {
                m_vo = new ContactVo();
                // names comes in hand sometimes
                String name = cur.getString(1);
                String emlAddr = cur.getString(3);
                System.err.println("Value Is---------->" + cur.getString(2)
                        + "Name--" + name + "Email---" + emlAddr);
                m_vo.setM_sName(name);
                m_vo.setM_sEmail(emlAddr);
                // keep unique only
                if (emlRecsHS.add(emlAddr.toLowerCase())) {
                    emlRecs.add(emlAddr);
                }
                m_arrList.add(m_vo);
            } while (cur.moveToNext());
        }

        cur.close();
        return m_arrList;
    }

编辑:

在显示所有联系人列表的列表视图的 onItemClick 事件中编写以下代码。

m_lvList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(m_context, m_arrContact.get(arg2).getM_sEmail(), Toast.LENGTH_LONG).show();
            Intent in = new Intent(m_context, Test.class);
            in.putExtra("pos", arg2);
            in.putParcelableArrayListExtra("parcel", m_arrContact);
            startActivity(in);
        }
    });

让您的 vo parcelable 将详细信息传递到另一个活动并访问它。

联系方式

    public class ContactVo implements Parcelable {
    private String m_sName, m_sEmail;

    public ContactVo() {
    }

    public ContactVo(Parcel in) {
        readFromParcel(in);
    }

    public String getM_sName() {
        return m_sName;
    }

    public void setM_sName(String m_sName) {
        this.m_sName = m_sName;
    }

    public String getM_sEmail() {
        return m_sEmail;
    }

    public void setM_sEmail(String m_sEmail) {
        this.m_sEmail = m_sEmail;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(m_sName);
        dest.writeString(m_sEmail);

    }

    public void readFromParcel(Parcel p_in) {

        m_sName = p_in.readString();
        m_sEmail = p_in.readString();
    }

    public static final Parcelable.Creator<ContactVo> CREATOR = new Parcelable.Creator<ContactVo>() {
        public ContactVo createFromParcel(Parcel s) {
            return new ContactVo(s);
        }

        public ContactVo[] newArray(int size) {
            return new ContactVo[size];
        }
    };

}

在您的另一项活动中,如下所示:

public class Test extends Activity {

    ArrayList<ContactVo> m_arr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_layout);

        m_arr = getIntent().getParcelableArrayListExtra("parcel");
        int pos = getIntent().getIntExtra("pos", 0);
        System.out.println("Detailsss------>" + m_arr.get(pos).getM_sEmail()
                + m_arr.get(pos).getM_sName());

    }

}

您可以在其中添加任意数量的详细信息并将其ArrayList<ContactVo>传递给另一个活动。

要获取其他联系方式,请查看这里

于 2013-11-12T09:36:52.110 回答