我怎样才能selectAndroid的联系人对话框的电话号码

我正在使用旧的联系人API来select联系人的电话号码。 我想使用较新的ContactsContracts API。 我想要…

  1. 显示所有具有电话号码的联系人的对话框
  2. …用户select一个联系人和他们的电话号码之一。
  3. …访问所选的电话号码。

ContactsContracts非常复杂。 我发现了很多例子,但没有一个符合我的需求。 我不想select联系人,然后查询联系人的详细信息,因为这会给我一个他们的电话号码列表。 我需要用户select一个联系人的电话号码。 我不想写我自己的对话框来显示联系人或让用户select一个电话号码。 有什么简单的方法来得到我想要的?

以下是我正在使用的旧API代码:

static public final int CONTACT = 0; ... Intent intent = new Intent(Intent.ACTION_PICK, Contacts.Phones.CONTENT_URI); startActivityForResult(intent, CONTACT); ... public void onActivityResult (int requestCode, int resultCode, Intent intent) { if (resultCode != Activity.RESULT_OK || requestCode != CONTACT) return; Cursor c = managedQuery(intent.getData(), null, null, null, null); if (c.moveToFirst()) { String phone = c.getString(c.getColumnIndexOrThrow(Contacts.Phones.NUMBER)); // yay } } 

Solutions Collecting From Web of "我怎样才能selectAndroid的联系人对话框的电话号码"

  Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); startActivityForResult(intent, PICK_CONTACT); 

这些代码可以帮助你,我认为PICK活动只返回所选联系人的ID。 您可以从中查询联系人提供商,如果有多个电话号码,请提示用户select其中的一个。

你也可以使用这个(更新):

  public void readcontact(){ try { Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/people")); startActivityForResult(intent, PICK_CONTACT); } catch (Exception e) { e.printStackTrace(); } } public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case (PICK_CONTACT) : if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = managedQuery(contactData, null, null, null, null); startManagingCursor(c); if (c.moveToFirst()) { String name = c.getString(c.getColumnIndexOrThrow(People.NAME)); String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER)); perrsonname.setText(name); Toast.makeText(this, name + " has number " + number, Toast.LENGTH_LONG).show(); } } break; } } 

嗨28/12更新内容:你可以使用这个:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case CONTACT_PICKER_RESULT: final EditText phoneInput = (EditText) findViewById(R.id.phoneNumberInput); Cursor cursor = null; String phoneNumber = ""; List<String> allNumbers = new ArrayList<String>(); int phoneIdx = 0; try { Uri result = data.getData(); String id = result.getLastPathSegment(); cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null); phoneIdx = cursor.getColumnIndex(Phone.DATA); if (cursor.moveToFirst()) { while (cursor.isAfterLast() == false) { phoneNumber = cursor.getString(phoneIdx); allNumbers.add(phoneNumber); cursor.moveToNext(); } } else { //no results actions } } catch (Exception e) { //error actions } finally { if (cursor != null) { cursor.close(); } final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]); AlertDialog.Builder builder = new AlertDialog.Builder(your_class.this); builder.setTitle("Choose a number"); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { String selectedNumber = items[item].toString(); selectedNumber = selectedNumber.replace("-", ""); phoneInput.setText(selectedNumber); } }); AlertDialog alert = builder.create(); if(allNumbers.size() > 1) { alert.show(); } else { String selectedNumber = phoneNumber.toString(); selectedNumber = selectedNumber.replace("-", ""); phoneInput.setText(selectedNumber); } if (phoneNumber.length() == 0) { //no numbers found actions } } break; } } else { //activity result error actions } } 

你需要适应这个与你的应用程序一起工作

在这里你可以find一个伟大的代码: http : //developer.android.com/training/basics/intents/result.html

 static final int PICK_CONTACT_REQUEST = 1; // The request code ... private void pickContact() { Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request it is that we're responding to if (requestCode == PICK_CONTACT_REQUEST) { // Make sure the request was successful if (resultCode == RESULT_OK) { // Get the URI that points to the selected contact Uri contactUri = data.getData(); // We only need the NUMBER column, because there will be only one row in the result String[] projection = {Phone.NUMBER}; // Perform the query on the contact to get the NUMBER column // We don't need a selection or sort order (there's only one result for the given URI) // CAUTION: The query() method should be called from a separate thread to avoid blocking // your app's UI thread. (For simplicity of the sample, this code doesn't do that.) // Consider using CursorLoader to perform the query. Cursor cursor = getContentResolver() .query(contactUri, projection, null, null, null); cursor.moveToFirst(); // Retrieve the phone number from the NUMBER column int column = cursor.getColumnIndex(Phone.NUMBER); String number = cursor.getString(column); // Do something with the phone number... } } } 

从旧的答案和我自己的testing,我结束了这个:

启动联系人列表:

 import android.content.Intent; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; 

 public static final int PICK_CONTACT = 100; 

 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); intent.setType(Phone.CONTENT_TYPE); //should filter only contacts with phone numbers startActivityForResult(intent, PICK_CONTACT); 

onActivityResult处理程序:

 private static final String[] phoneProjection = new String[] { Phone.DATA }; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (PICK_CONTACT != requestCode || RESULT_OK != resultCode) return; Uri contactUri = data.getData(); if (null == contactUri) return; //no tampering with Uri makes this to work without READ_CONTACTS permission Cursor cursor = getContentResolver().query(contactUri, phoneProjection, null, null, null); if (null == cursor) return; try { while (cursor.moveToNext()) { String number = cursor.getString(0); // ... use "number" as you wish } } finally { cursor.close(); } // "cursor" is closed here already } 

那么Rizvan的答案有什么不同呢?

在我的testing设备(三星S3)上:

  • 该应用程序不需要READ_CONTACS权限(因为我使用返回的uri ,当我只使用它的“ID”,并创buildselectID =?查询types,权限崩溃发生)
  • 当联系人有多个电话号码时,select器本身确实显示对话框来仅select其中的一个,然后它返回uri ,直接导向该单个select的号码
  • 即使某些电话将uri返回给多个号码,build议的onActivityResult处理程序也可以扩展为全部读取,您可以执行自己的select对话框。

所以这对我来说就像完全适合OP所要求的。

现在我只是想知道:

  1. 在哪些手机上,这将需要READ_CONTACTS权限(根据http://developer.android.com/guide/topics/providers/content-provider-basics.html#Intents不应该&#xFF09;
  2. 在哪些电话上它将返回多个数字而不是做select对话框

让我知道,如果你有真实世界的经验,谢谢。

更新:HTC Desire S,自定义ROM与Android 4.0.3 – >有两个问题,需要READ_CONTACTS权限的工作,并将返回多个号码,没有额外的select对话框。