如何使用Android的联系人对话框选择电话号码
我正在使用旧的联系人API来选择具有电话号码的联系人。我想使用较新的ContactsContracts API。我想...如何使用Android的联系人对话框选择电话号码
- ...显示对话框,包含所有具有电话号码的联系人。
- ...用户选择一个联系人和他们的电话号码之一。
- ...访问选定的电话号码。
ContactsContracts非常复杂。我发现了很多例子,但没有一个符合我的需求。我不想选择联系人,然后查询联系人的详细信息,因为这会给我一个他们的电话号码列表。我需要用户选择一个联系人的电话号码。我不想写我自己的对话框来显示联系人或让用户选择一个电话号码。有什么简单的方法来获得我想要的吗?
这里是我使用旧的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
}
}
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
这些代码可以帮助U,我认为PICK活动仅返回联系人的ID采摘。 从中可以查询联系人提供商,如果有多个电话号码,请提示用户选择其中一个。
U可以也用这个(更新):
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的更新内容: U可以使用此:
@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
}
}
你需要调整它以适应你的应用程序
你是说没有内置的方式选择一个电话号码? – NateS 2011-12-23 07:37:33
只是选择一个联系人很好,但我需要选择一个联系人的电话号码。使用旧API的问题中的代码显示联系人选择器,当显示联系人时,如果它有多个电话号码,则会显示一个小对话框以选择电话号码(至少在我的HTC Thunderbolt上)。这是我想要做的,但用新的API。我不知道此功能是否内置,或者是否需要自定义对话框来选择电话号码。 – NateS 2011-12-27 18:45:45
http://developer.android.com/resources/samples/BusinessCard/src/com/example/android/businesscard/index.html使用这个链接的联系人选择ñ甚至为数ni试图做它的代码。很高兴 – Rizvan 2011-12-28 10:32:42
从旧的答案和我自己的测试,最后我用这个:
启动联系人列表:
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的答案有什么不同?
在我的测试设备(三星S3):
- 的应用程序不会不需要
READ_CONTACS
许可(因为我使用返回uri
的是,当我只用它的“身份证”,并创建选择ID =?查询类型,允许崩溃发生) - 当联系人有多个电话号码,捡拾工具本身并显示对话框,选择只是其中之一,则返回
uri
这直接导致了一个选择的号码 - EV如果某个电话将返回
uri
为多个数字,则可以扩展提议的onActivityResult处理程序以将其全部读取,并且您可以执行自己的选择对话框。
所以这对我来说很像OP所要求的。
现在,我只是想知道:
- 上的手机,这将需要
READ_CONTACTS
许可(它不应该,根据http://developer.android.com/guide/topics/providers/content-provider-basics.html#Intents) - 在其手机将返回,而不是在做选择对话框多个号码
让我知道如果你有真实世界的经验,谢谢。
更新: HTC Desire S,自定义ROM与Android 4.0.3 - >有两个问题,需要READ_CONTACTS权限的工作,并将返回多个数字,没有额外的选择对话框。
在这里你可以找到一个伟大的代码: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...
}
}
}
上有那么对于同样的问题不够。 – st0le 2011-12-23 05:57:04
可能重复的[如何调用Android联系人列表?](http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list) – st0le 2011-12-23 05:59:32
其他问题选择一个联系人,他们不选一个电话号码。 – NateS 2011-12-23 07:36:37