如何一次选择多个联系人?

时间:2011-04-15 10:57:11

标签: android database android-contacts android-contentresolver

我使用以下代码检索电话簿联系人到我的应用程序:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
                                            Contacts.CONTENT_URI);  
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);      
}

但我想选择多个联系人并将其上传到数据库。这是可能的,如果可以,我该怎么做?

2 个答案:

答案 0 :(得分:1)

您可以直接在活动中查询内容提供商,以便用户可以选择多个联系人。这可以使用联系人合同来实现。有关详细信息,请查看api documentation或此tutorial blog post

答案 1 :(得分:0)

试试这段代码..

     people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    int position=0;
    Cursor q=db.query(mProfile,new String[]{"person_name"},"person_name"+"!='"+null+"'",null,null, null, null);
    people.moveToFirst();
        int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);    
        while(!people.isAfterLast()) {
            Cursor o=db.query(mProfile,new String[]{"person_name"},"person_name"+"='"+people.getString(nameFieldColumnIndex)+"'",null,null, null, null);
            if(!(o.getCount()>0))
            {  
            mConname.add(position, people.getString(nameFieldColumnIndex));
            try{
                String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
                String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                if ( hasPhone.equalsIgnoreCase("1"))
                    hasPhone = "true";
                else
                    hasPhone = "false" ;
                if (Boolean.parseBoolean(hasPhone)) 
                {
                    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                    while (phones.moveToNext()) 
                    {
                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        mConno.add(position,phoneNumber);

                    }
                    phones.close(); 
                }   
                if(hasPhone=="false")
                {   mConname.remove(position);
                }   
                else
                    position++;
            }       
            catch(Exception e)
            { 

            }
        }
            people.moveToNext();
        }

使用sqlite并存储它