3

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

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

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

4

2 回答 2

1

那么您可以直接在您的活动中查询内容提供者,以便用户可以选择多个联系人。这可以使用联系人合约来实现。有关更多信息,您可以查看api 文档或本教程博客文章

于 2011-04-15T11:14:19.353 回答
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 并存储它

于 2011-04-15T11:46:55.997 回答