2
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));

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

4

3 回答 3

1
    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();
于 2012-05-22T06:15:51.683 回答
1

试试下面的,

public void getContactDetails(String conatctname)
    {
        try
        {
            ContentResolver cr =getContentResolver();
            Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            while (cursor.moveToNext()) 
            {

                FirstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                if(FirstName!=null)
                {
                    try
                    {
                    String[] splitval=FirstName.split(" ");
                    if(splitval.length>=1)
                    {
                        FirstName=splitval[0];
                        if(FirstName.equals(conatctname))
                        {
                            if(Integer.parseInt(cursor.getString(cursor.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()) 
                            {
                            PhoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            PhoneNumberArray.add(PhoneNumber);
                            }
                            pCur.close();
                        }

                    }
                    }
                    catch(Exception error)
                    {
                        Log.d("SplitError", error.getMessage());
                    }                   

            }
            cursor.close();
        }
        catch (NumberFormatException e)
        {
            e.printStackTrace();
        }
    }
于 2012-05-22T06:17:20.057 回答
0

您可以从电话联系人中获取选定的姓名和号码

     Uri uri = data.getData();
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER};

        Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null);

        int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        people.moveToFirst();
        do {
            String name   = people.getString(indexName);
            String number = people.getString(indexNumber);
            System.out.println(name+number);

        } while (people.moveToNext());
于 2016-06-02T07:09:22.490 回答