呼叫显示所有联系人的联系人选择器已完成(如此在SO上多次说明):
Intent intent = new Intent( Intent.ACTION_PICK, Contacts.CONTENT_URI );
startActivityForResult( intent, REQ_CODE );
Run Code Online (Sandbox Code Playgroud)
我在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 ) );
}
Run Code Online (Sandbox Code Playgroud)
拨打联系人选择器并仅显示具有电话号码的联系人可以这样做(也可在SO上找到):
Intent intent = new Intent( Intent.ACTION_PICK );
intent.setType( ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE );
startActivityForResult( intent, REQ_CODE );
Run Code Online (Sandbox Code Playgroud)
如果我这样做,我只会在联系人选择器中看到那些至少有一个电话号码的联系人,这正是我需要的.不幸的是,使用上面的代码片段我只得到显示名称,但不再是任何电话号码.
有没有人知道我需要改变什么来获取电话号码?
提前致谢
将 where 子句中的 Phone.Contact_Id 更改为 Phone._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 ) );
}
Run Code Online (Sandbox Code Playgroud)
这个问题有更多细节。
希望它有帮助:)
| 归档时间: |
|
| 查看次数: |
1046 次 |
| 最近记录: |