让用户选择联系人并获取联系人的手机号码

时间:2011-08-20 23:57:06

标签: android contacts

我正在尝试让用户从他的联系人列表中选择一个联系人,以便我可以对该号码进行一些处理。我启动列表选择活动的代码是

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,  
            Contacts.Phones.CONTENT_URI);  
    startActivityForResult(contactPickerIntent, PICK_CONTACT);

我的接收代码是

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
    case (PICK_CONTACT) :
        if (resultCode == Activity.RESULT_OK) {

            Bundle extras = data.getExtras();  
            Set<String> keys = extras.keySet();  
            Iterator<String> iterate = keys.iterator();  
            while (iterate.hasNext()) {  
                String key = iterate.next();  
                Log.v(TAG, key + "[" + extras.get(key) + "]");  
            }  
            Uri result = data.getData();  
            Log.v(TAG, "Got a result: "  
                + result.toString());  
        }
    break;
    }
}

但附加内容包中没有任何名称或电话号码。如何获得用户选择的结果?

1 个答案:

答案 0 :(得分:1)

getData()调用包含可以打开游标的Uri。请尝试以下方法:

   Uri resultUri = data.getData();
   Cursor cursor =  managedQuery(resultUri, null, null, null, null);
   if (cursor.moveToFirst()) {
       // Found results process cursor
   }