【问题标题】:How can i know a number has a country code?我怎么知道一个号码有国家代码?
【发布时间】:2016-04-20 15:44:49
【问题描述】:

我正在使用电话号码进行注册。系统与whatsapp完全相同。

  1. 用户给他写号码。
  2. 服务器向该号码发送短信
  3. 用户验证他的号码。
  4. 然后用户的通讯录发送到服务器寻找他的朋友。

问题从这里开始。这将是一个国际应用程序。您知道每个国家/地区都有不同的电话号码格式。我将像这样存储所有数字:123456789

如果用户已经将国家代码放在号码的前面,那么没有问题。但如果他不这样做,我该怎么办?我怎么知道一个号码有国家代码?如果我知道,那么我可以输入地址簿所有者的国家代码。因为如果一个号码没有国家代码,那么他们就住在同一个国家。

【问题讨论】:

  • 进行 2 个单独的输入,一个用于国家代码,一个用于实际号码。这可能是您的问题最通用的解决方案
  • 不,我不是这个意思。我的意思是从通讯录中获取。(来自联系人应用程序)
  • 带国家代码的数字将以+开头,对吗?然后将不是从+ 开始的数字作为用户输入,您可以在您的数字数据库中搜索以查找以+ 开头并包含作为输入的数字的数字,这些数字与输入中的顺序相同。如果找到某些东西 - 您将输入数据加入搜索结果。如果没有 - 您只需将输入保存为没有国家代码的数字。对不起,如果我误解了这个问题。
  • 但是 +905383708177 和 905383708177 是一样的,所以你可以毫无问题地调用它们。所以 + 不是灵丹妙药

标签: phone-number


【解决方案1】:

使用这段代码的 sn-p 并记得添加 libphonenumber 8.8.1 库:

//Get the International Code
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneNumberUtil mPhone = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber mNumber;

//Get the phone no
EditText editText = (EditText) findViewById(R.id.usr_phone_no);
//Remove all 0's in the left and non-numeral characters and parse it to an international number format
String userPhoneNumber = Long.valueOf(editText.getText().toString().replaceAll("[^\\d]", "")).toString();
mNumber = mPhone.parse(userPhoneNumber, tm.getSimCountryIso().toUpperCase());

//Transform it the way you will use it (In this case, Long)
Long phoneNumber = Long.valueOf(String.format("%d%d",mNumber.getCountryCode(),mNumber.getNationalNumber()));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多