如何从选定的联系人获取电话号码? [英] How to get the phone number from selected contact?

查看:84
本文介绍了如何从选定的联系人获取电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void onActivityResult(int reqCode, int resultCode, Intent data) {

    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
    case (1) :
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cursor =  managedQuery(contactData, null, null, null, null);
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            Cursor phones = cr.query(Phone.CONTENT_URI, null, null, null, null);
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    //String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){
                        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                             new String[]{id}, null);
                        //while (pCur.moveToNext()) {
                            // Do something with phones
                            //nameView = (TextView) findViewById(R.id.textView4);
                            //nameView.setText(name.toString());
                        //} 
                        pCur.close();
                    }
                }
            }

            cursor.moveToFirst();
            String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
            //String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));

我想从所选联系人中检索电话号码,并且我成功检索了联系人姓名,但是对于该号码,我仍然无法获取...有人可以帮助我在编码部分中从中检索电话号码吗?选择的联系人?

I would like to retrieve the phone number from the selected contact and I successfully retrieve the contact name but for the number I still cannot get it... Can someone please help me in the coding part for retrieving the phone number from the selected contact?

推荐答案

    final Uri Person = Uri.withAppendedPath(
            ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
            Uri.encode(number));

    final String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };

    final Cursor cursor = context.getContentResolver().query(Person, projection,
            null, null, null);

    if (cursor.moveToFirst()) {

        final String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        return number;
    }
    cursor.close();

这篇关于如何从选定的联系人获取电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆