搜索多个电话号码的联系人

时间:2014-01-12 12:12:07

标签: android

我想要一系列电话号码,以便与手机上的联系人进行匹配。这样做最有效的方法是什么?所以基本上输出应该是与这些电话号码匹配的联系人数组。

我知道ContactsContract.PhoneLookup,但据我所知,它一次只查找一个数字。有没有更有效的方法一次搜索多个数字?

谢谢。

1 个答案:

答案 0 :(得分:0)

我会从我的项目中给你一些代码。看看它,并编辑你想要的。 :)

        final Uri uri = ContactsContract.Contacts.CONTENT_URI;
        final String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };

        String selection  = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
        final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        Cursor cur = getContentResolver().query(uri, projection, selection,  null, sortOrder);

        int Size = cur.getCount();
        con = new Contact[Size];
        for (int z = 0; z < Size; z++)
            con[z] = new Contact();

        conArray = new ArrayList<Contact>();


        if (Size > 0) 
        {
            while (cur.moveToNext()) 
            {
                String Sid = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, Sid);
                InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),my_contact_Uri);            
                BufferedInputStream buf = new BufferedInputStream(photo_stream);
                Bitmap my_btmp = BitmapFactory.decodeStream(buf);

                // get the phone number
                Cursor pCur = getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                        null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                        + " = ?", new String[] { Sid }, null);

                con[CountCon].Add(name);
                if (my_btmp != null)
                    con[CountCon].SetImage(my_btmp);
                else 
                    con[CountCon].SetImage(bm);

                while (pCur.moveToNext()) 
                {
                    con[CountCon].Add(pCur.getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                }
                if (con[CountCon].GetNumber(0).isEmpty())
                    con[CountCon].Add("none");

                conArray.add(con[CountCon]);

                CountCon++;

                pCur.close();
            }
        }
        cur.close();