gpt4 book ai didi

Android:联系人选择器 Intent |无法实例化 Uri 类型

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:17 24 4
gpt4 key购买 nike

我正在尝试仅选择电话号码的联系人。我正在关注 this code

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, new Uri("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

但不幸的是,它显示了一个错误:Cannot instantiate the type Uri

实际上我有另一个工作代码,它运行良好,但在选择电子邮件联系人时崩溃。我只需要电话号码。

Intent intentContact = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
intentContact.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intentContact, PICK_CONTACT);

并且在 onReceive() 处,调用此方法

public void getContactInfo(Intent intent) {

ContentResolver cr = getContentResolver();
cursor = cr.query(intent.getData(), null, null, null, null);

while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer
.parseInt(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
} else {
snipp.showAlertDialog(getApplicationContext(), "No Number",
"Cannot read number", false);
}

}
cursor.close();
}

最佳答案

这对我有用:

private void pickContact() {
Intent pickContactIntent = new Intent( Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI );
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

编辑:

您的 onActivityResult() 应该如下所示:

@Override
public void onActivityResult( int requestCode, int resultCode, Intent intent ) {

super.onActivityResult( requestCode, resultCode, intent );
if ( requestCode == PICK_CONTACT_REQUEST ) {

if ( resultCode == RESULT_OK ) {
Uri pickedPhoneNumber = intent.getData();
// handle the picked phone number in here.
}
}
}
}

关于Android:联系人选择器 Intent |无法实例化 Uri 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12752388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com