我构建了一个ContactList应用程序,我正在尝试了解如何处理多个电话号码的联系人。我已经看到本机电话应用程序显示一个包含所有数字的PopUp窗口和一个带有“记住此选项”文本的复选框。是否可以显示相同的PopUp?我怎么能这样做?
这就是我目前获取电话号码的方式(此代码实际上遍历所有用户的号码并将其全部调用):
Cursor phoneCursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{cursor.getString(ContactsQuery.ID)}, null);
phoneCursor.moveToNext();
while (phoneCursor.moveToNext()) {
String normalizedPhoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
((MainActivity) getActivity()).callNumber(normalizedPhoneNumber);
}
答案 0 :(得分:0)
看一下拨号器应用的代码可以帮助您实现目标。
请参阅在线源代码PhoneNumberInteraction.java或
git clone https://android.googlesource.com/platform/packages/apps/Dialer
然后打开 DialtactsActivity :
src/com/android/dialer/interactions/PhoneNumberInteraction.java
PhoneNumberInteraction
课程会发起电话或短信。如果有多个候选者,则此类显示一个对话框以选择一个。
应该通过静态工厂方法来完成其中一个交互:即参见:startInteractionForPhoneCall
使用给定的联系人Uri开始调用操作。如果有多个电话候选人,则会自动显示对话框,并要求用户选择一个。
PhoneDisambiguationDialogFragment是一个DialogFragment,用于显示一个对话框,其中包含一个电话号码列表,其中一个电话号码将被选择拨打电话或发起短信。
public static class PhoneDisambiguationDialogFragment extends DialogFragment
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Activity activity = getActivity();
mPhoneList = getArguments().getParcelableArrayList(ARG_PHONE_LIST);
mInteractionType = getArguments().getInt(ARG_INTERACTION_TYPE);
mCallOrigin = getArguments().getString(ARG_CALL_ORIGIN);
mPhonesAdapter = new PhoneItemAdapter(activity, mPhoneList, mInteractionType);
final LayoutInflater inflater = activity.getLayoutInflater();
final View setPrimaryView = inflater.inflate(R.layout.set_primary_checkbox, null);
return new AlertDialog.Builder(activity)
.setAdapter(mPhonesAdapter, this)
.setTitle(mInteractionType == ContactDisplayUtils.INTERACTION_SMS
? R.string.sms_disambig_title : R.string.call_disambig_title)
.setView(setPrimaryView)
.create();
}
适配器mPhonesAdapter
是联系人电话号码列表的列表适配器,setPrimaryView
(R.layout.set_primary_checkbox
)是"请记住此选项" 复选框。
请注意,当多个候选人时,DisambiguationDialog
会弹出:
见
@Override
public void (Loader<Cursor> loader, Cursor cursor) {
摘录:
if (phoneList.size() == 0) {
onDismiss();
} else if (phoneList.size() == 1) {
PhoneItem item = phoneList.get(0);
onDismiss();
performAction(item.phoneNumber);
} else {
// There are multiple candidates. Let the user choose one.
showDisambiguationDialog(phoneList);
}
和
void showDisambiguationDialog(ArrayList<PhoneItem> phoneList) {
PhoneDisambiguationDialogFragment.show(((Activity)mContext).getFragmentManager(),
phoneList, mInteractionType, mCallOrigin);