调用联系人选择器显示所有联系人已完成(如此在SO上多次说明):
Intent intent = new Intent( Intent.ACTION_PICK, Contacts.CONTENT_URI );
startActivityForResult( intent, REQ_CODE );
我在onActivityResult中获取了联系人姓名及其所有电话号码,并附有以下片段:
public void onActivityResult( int requestCode, int resultCode, Intent intent )
{
Uri contactUri = intent.getData();
ContentResolver resolver = getContentResolver();
long contactId = -1;
// get display name from the contact
Cursor cursor = resolver.query( contactUri,
new String[] { Contacts._ID, Contacts.DISPLAY_NAME },
null, null, null );
if( cursor.moveToFirst() )
{
contactId = cursor.getLong( 0 );
Log.i( "tag", "ContactID = " + Long.toString( contactId ) );
Log.i( "tag", "DisplayName = " + cursor.getString( 1 ) );
}
// get all phone numbers with type from the contact
cursor = resolver.query( Phone.CONTENT_URI,
new String[] { Phone.TYPE, Phone.NUMBER },
Phone.CONTACT_ID + "=" + contactId, null, null );
while( cursor.moveToNext() )
{
Log.i( "tag", "PhoneNumber = T:" + Integer.toString( cursor.getInt( 0 ) ) + " / N:" + cursor.getString( 1 ) );
}
拨打联系人选择器并仅显示带有电话号码的联系人可以这样做(也可以在SO上找到):
Intent intent = new Intent( Intent.ACTION_PICK );
intent.setType( ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE );
startActivityForResult( intent, REQ_CODE );
如果我这样做,我只会在联系人选择器中看到那些至少有一个电话号码的联系人,这正是我所需要的。不幸的是,通过上面的代码片段,我只得到显示名称,但不再是任何电话号码。
是否有人知道我需要更改哪些内容才能获取电话号码?
提前致谢
答案 0 :(得分:2)
更改Phone._ID的where子句中的Phone.Contact_Id,如下所示:
cursor = resolver.query( Phone.CONTENT_URI,
new String[] { Phone.TYPE, Phone.NUMBER },
Phone._ID + "=" + contactId, null, null );
while( cursor.moveToNext() )
{
Log.i( "tag", "PhoneNumber = T:" + Integer.toString( cursor.getInt( 0 ) ) + " / N:" + cursor.getString( 1 ) );
}
此question中的详细信息。
希望有所帮助:)