如何从模拟器访问联系人

时间:2011-07-05 06:29:24

标签: android

我想将android模拟器中保存的联系人显示给我的应用程序。联系人列表应该显示为名字,姓氏等。我想为所有领域创建单独的列表。请帮助我。提前谢谢。

1 个答案:

答案 0 :(得分: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();
    }

也可以去看看这个教程(Working with Android Contacts)你可以有一个明确的想法.. 另请参阅本教程(query-contacts-in-android