gpt4 book ai didi

android - 从列表中选择联系人时,从地址簿中填充联系人号码有效,但从搜索结果中选择时失败

转载 作者:行者123 更新时间:2023-11-29 18:20:51 24 4
gpt4 key购买 nike

我使用以下代码显示联系人列表,然后通过选择联系人来检索联系人号码。但是,在列表中搜索联系人然后从搜索结果中选择时,没有检索到任何号码。你知道我错过了什么吗?任何帮助表示赞赏。感谢阅读。

 private void registerLongClick() {
TextView vonage_number = (TextView) findViewById(R.id.vonage_number);
vonage_number.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Perform action on click
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, 1001);
return true;
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String DEBUG_TAG = Constants.CALL_LOG_TAG_NAME;
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1001:
Cursor cursor = null;
String phone = "";
try {
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: "
+ result.toString());

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

//query for phone number
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone._ID + " = " + id, null,
null);

int phoneIdx = cursor.getColumnIndex(Phone.DATA);

if (cursor.moveToFirst()) {
phone = cursor.getString(phoneIdx);
Log.v(DEBUG_TAG, "Got phone: " + phone);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get phone data", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText phoneEntry = (EditText) findViewById(R.id.vonage_number);
phoneEntry.setText(phoneEntry.getText().toString().trim()+phone);
if (phone.length() == 0) {
Toast.makeText(this, "No phone found for contact.",
Toast.LENGTH_LONG).show();
}

}

break;
}

} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}

最佳答案

尝试以下方法来尝试 {} catch {}。

try {
Uri contactData = data.getData();
Log.debug("Got a contact result: " + contactData.toString());
Cursor lookupCursor = managedQuery(contactData, null, null, null, null);
if (lookupCursor.moveToFirst()) {
String lookupKey = lookupCursor.getString(lookupCursor.getColumnIndexOrThrow(android.provider.ContactsContract.PhoneLookup.LOOKUP_KEY));
Log.debug("Lookup key: " + lookupKey);

cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY + " LIKE '" + lookupKey + "'", null, null);
startManagingCursor(cursor);

if (cursor.moveToFirst()) {
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
phone = cursor.getString(phoneIdx);
Log.debug("Got phone: " + phone);
cardNumber.setText(cardNumber.getText().toString().trim()+phone);
if (phone.length() == 0) {
Toast.makeText(this, "No phone found for contact.", Toast.LENGTH_LONG).show();
}
}
else {
Log.debug("No results");
}
} } catch (Exception e) {
Log.error("Failed to get phone data:" + e.getMessage()); }

关于android - 从列表中选择联系人时,从地址簿中填充联系人号码有效,但从搜索结果中选择时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5557808/

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