通过电话号码的唯一联系人android [英] Unique Contacts by phone number android

查看:96
本文介绍了通过电话号码的唯一联系人android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过手机号码(如聊天应用程序)过滤联系人.如果移动电话号码保存在联系人数据库中,并且带有国家(地区)代码和不含国家(地区)代码两次.我需要将两个数字合并为单个联系人.

How to filter contacts by mobile number like chat applications. If a mobile number saved in contacts database with twice by with country code and without country code. I need to merge two numbers as single contact.

例如,我用9876543210和+919876543210保存了联系人Bala.在这种情况下,我需要同时过滤两个数字,并将+919876543210数字返回到我的应用数据库.

For example, I saved a contact Bala with 9876543210 and +919876543210. In this scenario i need to filter both numbers and return +919876543210 number to my app database.

推荐答案

您需要将所有电话号码转换为E164格式(带国家代码,无空格或破折号)例如:+12125551234

You need to convert all phone numbers to E164 format (with country code, no spaces or dashes) e.g: +12125551234

如果您要定位API级别21和更高级别,则只需执行以下操作:

If you're targeting API level 21 and above, you can simply do:

String e164 = PhoneNumberUtils.formatNumberToE164(originalNumber, countryCode);

要获取国家/地区代码,请尝试以下操作:

To get the country code, try this:

TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = telephony.getSimCountryIso().toUpperCase();

如果您将api级别定位为21以下,则可以使用Google的 Android的libphonenumber库具有类似的API.

If you're targeting api level below 21, you can use google' libphonenumber library for Android with a similar API.

如果所有电话都已标准化为E164格式,则可以通过在联系人已经拥有该电话的情况下不向其添加新电话来删除重复项(您可能会找到

If all phones are normalized into E164 format, you can remove duplicates by simply not adding a new phone to a contact in case that contact already has that phone (you might find HashMap useful for this)

这篇关于通过电话号码的唯一联系人android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆