检索电话号码列表 onActivityResult
Posted
技术标签:
【中文标题】检索电话号码列表 onActivityResult【英文标题】:Retrieve List of Phone Numbers onActivityResult 【发布时间】:2019-12-12 23:18:01 【问题描述】:我正在使用来自GitHub
MultiContactPicker 的库,用于使用custom ListView
选择联系人。我可以获取用户选择的联系人姓名(onActivityResult),但无法获取电话号码。
图书馆使用private List<PhoneNumber> mPhoneNumbers = new ArrayList<>();
我试图获取此列表,但onActivityResult
不返回此列表。
调用 ContactPicker 时:
new MultiContactPicker.Builder(MainActivity.this) //Activity/fragment context
.theme(R.style.MyCustomPickerTheme) //Optional - default: MultiContactPicker.Azure
.hideScrollbar(false) //Optional - default: false
.showTrack(true) //Optional - default: true
.searchIconColor(Color.WHITE) //Option - default: White
.setChoiceMode(MultiContactPicker.CHOICE_MODE_MULTIPLE) //Optional - default: CHOICE_MODE_MULTIPLE
.handleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
.bubbleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
.bubbleTextColor(Color.WHITE) //Optional - default: White
.setTitleText("Select Contacts") //Optional - default: Select Contacts
.setSelectedContacts("10", "5" / myList) //Optional - will pre-select contacts of your choice. String... or List<ContactResult>
.setLoadingType(MultiContactPicker.LOAD_ASYNC) //Optional - default LOAD_ASYNC (wait till all loaded vs stream results)
.limitToColumn(LimitColumn.NONE) //Optional - default NONE (Include phone + email, limiting to one can improve loading time)
.setActivityAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
android.R.anim.fade_in,
android.R.anim.fade_out) //Optional - default: No animation overrides
.showPickerForResult(CONTACT_PICKER_REQUEST);
onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CONTACT_PICKER_REQUEST)
if(resultCode == RESULT_OK)
List<ContactResult> results = MultiContactPicker.obtainResult(data);
Log.d("MyTag", results.get(0).getDisplayName());
else if(resultCode == RESULT_CANCELED)
System.out.println("User closed the picker without selecting items.");
onActivityResult; List<ContactResult> results
默认只提供姓名,但我也想获取电话号码。
【问题讨论】:
在启动 MultiContactPicker 库时使用 limitToColumn(LimitColumn.PHONE) 谢谢,但我仍然无法理解onActivityResult
中的电话号码将如何返回
【参考方案1】:
我为你找到了这个github仓库AndroidContacts。
这个Repo的基本用法是:
从安卓设备获取所有联系人 从联系人获取特定数据 查询内部联系人 保存新联系人或
您也可以通过ContentResolver
使用此代码获取电话号码:
final ContentResolver cr = getContentResolver();
String[] projection = new String[] Contacts.DISPLAY_NAME, Phone.NUMBER;
final Cursor c = cr.query(Data.CONTENT_URI, projection, null, null, null);
myCursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] Phone.NUMBER, new int[]R.id.TVRow, 0);
myPhoneList.setAdapter(myCursorAdapter);
myPhoneList.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
c.moveToPosition(position);
Toast.makeText(getApplicationContext(), c.getString(1), Toast.LENGTH_SHORT).show();
);
为了更好的理解去官方documentation
Ans reference:如果你想在这里是answer already given
【讨论】:
OP 要求一个特定的库@hamad 我已经尝试了您提供的代码以及链接。我想你没有理解这里的场景。我没有填充 ListView 来显示所有名称和数字。相反,用户从联系人列表中选择了一些联系人(在库中实现)并拥有onActivityResult
,我可以在其中获取用户选择的联系人姓名,但我也想获取所选联系人的电话号码。【参考方案2】:
首先确保你已经在你的应用清单中写了这行权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
你的主要活动有这样的代码
new MultiContactPicker.Builder(MainActivity.this) //Activity/fragment context
.theme(R.style.MyCustomPickerTheme) //Optional - default: MultiContactPicker.Azure
.hideScrollbar(false) //Optional - default: false
.showTrack(true) //Optional - default: true
.searchIconColor(Color.WHITE) //Option - default: White
.setChoiceMode(MultiContactPicker.CHOICE_MODE_MULTIPLE) //Optional - default: CHOICE_MODE_MULTIPLE
.handleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
.bubbleColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)) //Optional - default: Azure Blue
.bubbleTextColor(Color.WHITE) //Optional - default: White
.setTitleText("Select Contacts") //Optional - default: Select Contacts
.setSelectedContacts("10", "5" / myList) //Optional - will pre-select contacts of your choice. String... or List<ContactResult>
.setLoadingType(MultiContactPicker.LOAD_ASYNC) //Optional - default LOAD_ASYNC (wait till all loaded vs stream results)
.limitToColumn(LimitColumn.NONE) //Optional - default NONE (Include phone + email, limiting to one can improve loading time)
.setActivityAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
android.R.anim.fade_in,
android.R.anim.fade_out) //Optional - default: No animation overrides
.showPickerForResult(CONTACT_PICKER_REQUEST);
希望对你有帮助
【讨论】:
是的,我显然已经获得了读取联系人的权限,因为我可以获得姓名。主要活动代码与我使用的相同,我已将其添加到问题中。那么电话号码在哪里?【参考方案3】:你只需要在onActivityResult中编写如下代码, 你可以像这样拨打电话号码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CONTACT_PICKER_REQUEST)
if(resultCode == RESULT_OK)
List<ContactResult> results = MultiContactPicker.obtainResult(data);
Log.d("MyTag", results.get(0).getDisplayName());
//contactsResults.addAll(MultiContactPicker.obtainResult(intent));
List<ContactResult> results = MultiContactPicker.obtainResult(intent);
if(!results.isEmpty())
for (int k = 0; k < results.size(); k++)
System.out.println(results.get(k).getPhoneNumbers().get(0).getNumber());
else if(resultCode == RESULT_CANCELED)
System.out.println("User closed the picker without selecting items.");
【讨论】:
以上是关于检索电话号码列表 onActivityResult的主要内容,如果未能解决你的问题,请参考以下文章
如何在 onActivityResult 中调用 startActivityForResult?