我正在使用旧的Contacts API来选择带有电话号码的联系人.我想使用较新的ContactsContracts API.我想要...
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
}
}
Run Code Online (Sandbox Code Playgroud)
Riz*_*van 36
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
Run Code Online (Sandbox Code Playgroud)
此代码可能对您有所帮助.我认为PICK操作只返回所选联系人的ID.从那里你可以查询联系提供商,如果有多个电话号码,提示用户选择其中一个.
您也可以使用它(更新):
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));
personname.setText(name);
Toast.makeText(this, name + " has number " + number, Toast.LENGTH_LONG).show();
}
}
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Updated 28/12 -2011
你可以用这个:
@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
}
}
Run Code Online (Sandbox Code Playgroud)
您需要对此进行调整才能使用您的应用.
Ela*_*lau 12
在这里,您可以找到以下优秀代码: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...
}
}
}
Run Code Online (Sandbox Code Playgroud)
Ped*_*d7g 10
从较旧的答案和我自己的测试结束后我用这个:
启动联系人列表:
import android.content.Intent;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
Run Code Online (Sandbox Code Playgroud)
...
public static final int PICK_CONTACT = 100;
Run Code Online (Sandbox Code Playgroud)
...
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);
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
那么Rizvan的答案有何不同?
在我的测试设备(三星S3)上:
READ_CONTACS的权限(因为我使用返回uri的是,当我只用它的"身份证",并创建选择ID =?查询类型,允许崩溃发生)uri哪个直接指向该单个选定的号码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权限才能工作,并且将返回多个数字而无需额外的选择对话框.
| 归档时间: |
|
| 查看次数: |
29126 次 |
| 最近记录: |