0

我正在使用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);

活动结果:

@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默认情况下只提供名称,但我也想获取电话号码。

4

3 回答 3

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();
    }
});

为了更好地理解去官方文档

Ans reference:如果你想在这里是已经给出的答案

于 2019-08-05T12:25:58.720 回答
0

你只需要在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.");
        }
    }
}}
于 2021-01-11T20:00:43.293 回答
0

首先确保您已在您的应用清单中写入此行并获得许可

<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);

希望对你有帮助

于 2019-08-05T12:27:31.647 回答