【问题标题】:How to get ISO Country code in android applications?如何在 Android 应用程序中获取 ISO 国家代码?
【发布时间】:2011-09-20 02:29:34
【问题描述】:

我是 android 应用程序的新开发人员。当我通过带有国家代码的手机号码时,我想获得 ISO 国家代码。如果我将手机号码作为 1-319-491-6338 传递,我可以在 android 中获取国家 ISO 代码为 US / USA 吗?

我写的代码如下:

      TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
      String countryCode = tm.getSimCountryIso();
      String mobileno="1-319-491-6338";

在这里,我在哪里可以传递手机号码?

有人可以帮帮我吗?

提前致谢

【问题讨论】:

  • 可能没有你想的那么简单。看看这个网站:countrycode.org。他们列出了至少 2 个不同的国家,电话代码为 1。在这种情况下,您将如何决定哪一个?
  • 字符串国家代码 = tm.getSimCountryIso();此行本身将 ISO 代码提供为 US

标签: android mobile code-generation country


【解决方案1】:

您可能无法通过标准 API 以编程方式查询国家/地区代码,但您可以在应用中包含一个表格。通过 Google 很容易找到这样的表格(例如http://countrycode.org/)。

Danger Will Robinson!:然而,你应该问问自己你想回答什么问题。您的问题暗示了国际拨号代码和 ISO 国家代码之间存在一对一映射的假设。这是正确的。例如,美国和加拿大都有国际拨号代码“1”。

或许可以考虑重新构建应用的界面。允许用户选择与电话号码关联的国家/地区,但使用http://countrycode.org/ 中的表格将最有可能的候选人排序在顶部?

【讨论】:

    【解决方案2】:

    遇到了同样的问题。最终我将所有数据放入excel并阅读了excel表。 这是实现:

    1. 将国家/地区代码表从http://countrycode.org/ 复制到Microsoft Excel 文件中。
    2. 在 \res\raw\countrycode_org.xls 中将 Excel 文件另存为 97-2003 兼容 (.xls)
    3. here下载JExcelApi
    4. 使用以下类读取文件:

      公共类 CountryCodes { 私有 HashMap mCountryByName = new HashMap(); 私有 HashMap mCountryByCode = new HashMap();; 私有 ArrayList mCountries = new ArrayList();

      public void addCountry(String countryName,String ISO_code,String countryCode){
          countryCode = PhoneNumberUtil.normalizeDigitsOnly(countryCode);
          Country country = new Country();
          country.Name = countryName;
          country.Code = countryCode;
          country.ISO_code = ISO_code;
          mCountryByName.put(countryName, country);
          mCountryByCode.put(countryCode, country);
          mCountries.add(country);
      
          return;
      }
      
      public Country getCountryByCode(String countryCode){
          countryCode = PhoneNumberUtil.normalizeDigitsOnly(countryCode);
          return mCountryByCode.get(countryCode);
      }
      
      public Country getCountryByName(String countryName){
          return mCountryByName.get(countryName);
      }
      
      public Country getCountryByIsoCode(String ISO_code){
          ISO_code = ISO_code.toUpperCase();
          for (Country country:mCountries){
              String [] strArr = country.ISO_code.split("/| ");
              for (String s:strArr){
                  if (ISO_code.equals(s))
                      return country;
              }
          }
          return null;
      }
      
      
      
      public  String[] getCountryNamesList(){
          String[] res = new String [mCountries.size()];
          int i=0;
          for (Country c:mCountries){
              res[i] = c.Name;
              i++;
          }
          return res;
      }
      
      
      
      public void readCountryCodesFromExcelWorkbook()
      {
          Context context = GlobalData.getInstance().getApp();
          Workbook mWorkbook;
          InputStream myRawResource = context.getResources().openRawResource(R.raw.countrycode_org);
          if (myRawResource == null)
              Toast.makeText(context,"XML file not found",Toast.LENGTH_LONG).show();
          else
              try {
                  WorkbookSettings ws = new WorkbookSettings();
                  ws.setEncoding("Cp1252");
      
                  mWorkbook = Workbook.getWorkbook(myRawResource);
                      //ArrayList<String[]> currentSheet = new ArrayList<String[]>();
                      Sheet sheet = mWorkbook.getSheet(0);
      
                      int rowsNum = sheet.getRows();
                      for (int rowNum = 1; rowNum < rowsNum; rowNum++) {
                          //Log.d("RowNum", ""+rowNum);
                          int colsNum = sheet.getColumns();
                          String[] strArr = new String[colsNum];
                          boolean rowIsFull = true;
                          for (int colNum = 0; colNum < colsNum; colNum++) {
                              strArr[colNum] = sheet.getCell(colNum, rowNum).getContents();
                              if (strArr[colNum].length() == 0)
                                  rowIsFull = false;
                          }
                          if (rowIsFull)
                              addCountry(strArr[0],strArr[1],strArr[2]);
                      }
      
      
              } catch (BiffException e) {
                  Toast.makeText(context,"Error Reading xml file: BiffException",Toast.LENGTH_LONG).show();
                  e.printStackTrace();
                  return ;
              } catch (IOException e) {
                  Toast.makeText(context,"Error Reading xml file: IOException",Toast.LENGTH_LONG).show();
                  e.printStackTrace();
                  return ;
              }
      }
      
      
      public Country[] getCountries(){
          return mCountries.toArray(new Country[0]);
      }
      
      
      
      public class Country {
          public String Name;
          public String Code;
          public String ISO_code;
      
      }
      

      }

    【讨论】:

      【解决方案3】:

      第一步 您可以在以下 URL 中获取 country calling code 及其 ISO name http://en.wikipedia.org/wiki/List_of_country_calling_codes

      http://www.unc.edu/~rowlett/units/codes/country.htm

      Step-2 您可以使用 java 程序获取该文件的页面源。您将获得 HTML 格式的文件

      第 3 步您可以使用任何可用的解析器将这些 HTML 文件转换为 XML 格式。见Open Source HTML Parsers in Java

      Step-4 填写电话号码即可获取电话号码。例如,如果号码是“1-319-491-6338”,那么呼叫代码是 1

      Step-5 将此调用代码与您从 XML 解析器获得的调用代码和国家/地区名称列表进行匹配。这样就可以得到iso国家

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        • 2018-06-30
        • 1970-01-01
        • 2015-03-07
        • 2011-12-22
        • 1970-01-01
        相关资源
        最近更新 更多