9

我试图让用户使用联系人选择器从联系人中选择电话号码。然而,现在我在网上看到的所有例子都显示了你如何选择一个联系人,但是我希望有一个第二个屏幕然后弹出,如果那个联系人有多个电话号码,那么你可以指定你想选择哪一个(方式该短信可让您在选择联系人时执行此操作)。使用联系人选择器从多个号码的用户中选择号码

我的问题是,你是否必须收集所有的数字,然后要求用户选择一个数字,或者这个功能已经内置到Android?我希望我只是忘记了一面旗帜或什么的。

回答

5

我能够通过创建第二个对话框来显示与联系人关联的所有电话号码。 首先,把这个地方在你的代码:)

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(intent, REQUEST_PICK_CONTACT); 

然后在onActivityResult(用这个来决定是否选择的联系人有多个电话号码,并显示一个对话框,如果是这样:

Uri result = data.getData(); 
Log.v(TAG, "Got a result: " + result.toString()); 

// get the contact id from the Uri 
String id = result.getLastPathSegment(); 

// query for phone numbers for the selected contact id 
c = getContentResolver().query(
    Phone.CONTENT_URI, null, 
    Phone.CONTACT_ID + "=?", 
    new String[]{id}, null); 

int phoneIdx = c.getColumnIndex(Phone.NUMBER); 
int phoneType = c.getColumnIndex(Phone.TYPE); 

if(c.getCount() > 1) { // contact has multiple phone numbers 
    final CharSequence[] numbers = new CharSequence[c.getCount()]; 
    int i=0; 
    if(c.moveToFirst()) { 
     while(!c.isAfterLast()) { // for each phone number, add it to the numbers array 
      String type = (String) Phone.getTypeLabel(this.getResources(), c.getInt(phoneType), ""); // insert a type string in front of the number 
      String number = type + ": " + c.getString(phoneIdx); 
      numbers[i++] = number; 
      c.moveToNext(); 
     } 
     // build and show a simple dialog that allows the user to select a number 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle(R.string.select_contact_phone_number_and_type); 
     builder.setItems(numbers, new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int item) { 
       String number = (String) numbers[item]; 
       int index = number.indexOf(":"); 
       number = number.substring(index + 2); 
       loadContactInfo(number); // do something with the selected number 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     alert.setOwnerActivity(this); 
     alert.show(); 

    } else Log.w(TAG, "No results"); 
} else if(c.getCount() == 1) { 
    // contact has a single phone number, so there's no need to display a second dialog 
} 

我知道这是一个古老的问题,但我希望它有帮助。

+0

我如何获得名称? – dmSherazi 2013-12-01 09:43:01

+0

它帮了很多,谢谢! – 2014-02-12 17:45:34

+0

帮助我,谢谢兄弟! – 2014-06-10 16:20:09

9

或者,您最初可以在联系人选取器中显示与每个联系人相关联的电话号码,然后从中选择一个。启动联系人选取这种方式(注意不是我的其他答案不同的URI):

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); 
startActivityForResult(intent, REQUEST_PICK_CONTACT); 

然后,在onActivityResult():

Uri result = data.getData(); 
Log.v(TAG, "Got a result: " + result.toString()); 

// get the phone number id from the Uri 
String id = result.getLastPathSegment(); 

// query the phone numbers for the selected phone number id 
Cursor c = getContentResolver().query(
    Phone.CONTENT_URI, null, 
    Phone._ID + "=?", 
    new String[]{id}, null); 

int phoneIdx = c.getColumnIndex(Phone.NUMBER); 

if(c.getCount() == 1) { // contact has a single phone number 
    // get the only phone number 
    if(c.moveToFirst()) { 
     phone = c.getString(phoneIdx); 
     Log.v(TAG, "Got phone number: " + phone); 

     loadContactInfo(phone); // do something with the phone number 

    } else { 
     Log.w(TAG, "No results"); 
    } 
} 
+0

更容易实现,更少的代码。 +1 – 2011-08-08 20:38:55

+2

此外,如果您添加一些代码,您也可以获得显示名称: 'int nameIdx = c.getColumnIndex(Phone.DISPLAY_NAME); //在phoneIdx创建的地方插入以下代码' 和 'contactName = c.getString(nameIdx); //插入下面的电话= c.getString(phoneIdx);' – 2012-12-24 20:46:07

+0

@howettl是否有任何URI传递将显示一个单一的对话框(在上面的URI做的联系人应用程序内)以选择电子邮件,还有电话号码? – cgr 2015-10-12 08:19:33

3

万一有人越过这再次跌倒。

另一个替代其他的答案是图书馆https://github.com/codinguser/android_contact_picker

全面披露:我这个库

+0

我必须找到一种方法来扩展你的图书馆来选择电子邮件(n中有1个)! – 2014-01-07 19:37:49

+0

我认为这样做应该相对容易 – codinguser 2014-01-09 13:40:38

+0

的确不那么容易。无论如何,我不会再失去这个库的时间,因为它对我来说根本不起作用(我正在使用Eclipse)。所以,我最好找一些其他的解决方案。 – 2014-02-12 16:06:15

0

它是简单的在Android开发人员解释引用的作者: https://developer.android.com/training/contacts-provider/modify-data.html#InsertEdit

和简单的附加码:

String phoneNumber = "+01 123 456 789"; 
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT); 
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); 
intent.putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber); 
if (intent.resolveActivity(getPackageManager()) != null) { 
startActivityForResult(intent, REQUEST_CODE_ADD_PHONE_CONTACT); 
} 

如果您需要活动结果,则必须通过REQUEST_CODE_ADD_PHONE_CONTACT变量监听Activity上的onActivityResult事件。

相关问题