获取具有lookupUri的联系人的所有电话号码

时间:2012-04-02 16:40:56

标签: android android-contacts android-cursor

我无法获得包含所有联系人电话号码的工作光标:

我使用这种方法获得lookupUri:

public static Uri getLookupUri (ContentResolver mContentResolver, String number) {
    Uri uri=null;
    Cursor contactLookupCursor =  
            mContentResolver.query(
                    Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
                            Uri.encode(number)), 
                            new String[] {PhoneLookup._ID, PhoneLookup.LOOKUP_KEY}, 
                            null,
                            null, 
                            null);

    if (contactLookupCursor.moveToNext()) {
         uri=Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_LOOKUP_URI, contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.LOOKUP_KEY)));
    }

    contactLookupCursor.close();
    return uri;
}

然后我想获得一个包含所有联系人电话号码的光标(手机):

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

    Cursor phones = localContentResolver.query(lookupUri, projection, null, null, null);

但我只获得IllegalArgumentException: Invalid column data1

3 个答案:

答案 0 :(得分:0)

您正在尝试从“联系人”行查询电话号码。联系人行没有电话号码。 ContactsContract.CommonDataKinds.Phone.NUMBER的值为“data1”,ContactsContract.Contacts表中没有“data1”列。

要查找联系人的所有电话号码,请执行以下操作:

找到您想要的联系人。你的getLookupUri可以这样做,除非你应该使用CONTENT_VCARD_URI调用withAppendedPath()。见Javadoc。

要获取联系人的所有电话号码,请使用Contacts.Entity表。

答案 1 :(得分:0)

以下代码将获取Android设备中的所有联系人。

public class GetContactFromLookup extends Activity {
    int CONTACTS_REQUEST_CODE =1;
    static ContentResolver cr;
    Activity thisActivity;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.test);
        thisActivity = this;
        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, 1);


            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          switch (requestCode) {
          case 1 :
            if (resultCode == Activity.RESULT_OK) {

            Uri uri = data.getData();
            List<String> items = uri.getPathSegments();
            String lookup_key = items.get(2);
            Log.v("", "id-->"+items.get(2));

            Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI ;//People.CONTENT_URI;
            Cursor mqCur =  managedQuery(myContacts, null, null, null, null);
            if(mqCur.moveToFirst())
            {              
                do
                {                  
                    String contact_id = mqCur.getString(mqCur
                            .getColumnIndexOrThrow(ContactsContract.Contacts.LOOKUP_KEY));
                    if(lookup_key.equals(contact_id)){  
                        String phone_no = mqCur.getString(mqCur
                                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                          Log.v("", "phone_no-->"+phone_no);
                    }                   
                }
                while(mqCur.moveToNext());
            }

            }           
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}

我认为这会对你有帮助。

答案 2 :(得分:0)

要使用联系人姓名获取电话号码,请使用此代码

 ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        "DISPLAY_NAME = '" + NAME + "'", null, null);
    if (cursor.moveToFirst()) {
        String contactId    =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        //
        //  Get all phone numbers.
        //
        Cursor phones = cr.query(Phone.CONTENT_URI, null,
            Phone.CONTACT_ID + " = " + contactId, null, null);
        while (phones.moveToNext()) {
            String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
            switch (type) {
                case Phone.TYPE_HOME:
                    // do something with the Home number here...
                    break;
                case Phone.TYPE_MOBILE:
                    // do something with the Mobile number here...
                    break;
                case Phone.TYPE_WORK:
                    // do something with the Work number here...
                    break;
            }
        }
        phones.close();
    }
    cursor.close();

我测试过,这是工作 我从这个答案得到它 How to get contacts' phone number in Android