如何在Android应用程序中获取ISO国家代码?

发布于 2024-11-16 17:12:02 字数 407 浏览 2 评论 0原文

我是 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";

这里,哪里可以传手机号码呢?

有人可以帮我吗?

提前致谢

I am a new developer on android application. I would like to get the ISO Country code when I pass the mobile number with country code. If I pass the mobile number as 1-319-491-6338, can I get country ISO code as US / USA in android?

I have written the code as follows:

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

Here, where can I pass the mobile number?

Can anybody please help me ?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆梦 2024-11-23 17:12:02

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

危险威尔·罗宾逊!:然而,人们应该问自己你想回答什么问题。您的问题隐含着这样的假设:国际拨号代码和 ISO 国家/地区代码之间存在一对一的映射。这是真实的。例如,美国和加拿大的国际拨号代码都是“1”。

也许考虑重新构建应用程序的界面。允许用户选择与电话号码关联的国家/地区,但使用 http://countrycode.org/ 中的表格来订购最可能的国家/地区高层候选人?

You may not be able to query the country code programmatically via the standard API but you could include a table in your app. Such a table is easily found via Google (e.g. http://countrycode.org/).

Danger Will Robinson!: However, one should ask yourself what question you are trying to answer. Implicit in your question is that assumption that there is a one-to-one mapping between international dialling codes and ISO country codes. This is not true. For example, both the USA and Canada have the international dialling code '1'.

Perhaps think about re-structuring your app's interface. Allow the user to select a country to associate with the phone number but use the table from http://countrycode.org/ to order the most likely candidates at the top?

╰つ倒转 2024-11-23 17:12:02

有同样的问题。最终我把所有的数据都放到了excel中,并读取了excel表。
实现如下:

  1. 将国家/地区代码表从 http://countrycode.org/ 复制粘贴到 Microsoft Excel 文件。
  2. 将 Excel 文件另存为 97-2003 兼容 (.xls),位于 \res\raw\countrycode_org.xls
  3. 下载 JExcelApi来自此处
  4. 使用下面的类来读取文件:

    公共类国家代码{
    私有 HashMap mCountryByName = new HashMap();
    私有 HashMap mCountryByCode = new HashMap();;
    私有 ArrayList mCountries = new ArrayList();

    public void addCountry(字符串国家名称,字符串ISO_code,字符串国家代码){
        国家/地区代码 = PhoneNumberUtil.normalizeDigitsOnly(国家/地区代码);
        国家/地区 = new Country();
        国家/地区.名称 = 国家/地区名称;
        国家/地区代码 = 国家/地区代码;
        国家/地区.ISO_code = ISO_code;
        mCountryByName.put(国家名称, 国家);
        mCountryByCode.put(国家代码, 国家);
        mCountries.add(国家);
    
        返回;
    }
    
    公共国家 getCountryByCode(字符串国家代码){
        国家/地区代码 = PhoneNumberUtil.normalizeDigitsOnly(国家/地区代码);
        返回 mCountryByCode.get(countryCode);
    }
    
    公共国家 getCountryByName(字符串国家名称){
        return mCountryByName.get(countryName);
    }
    
    公共国家 getCountryByIsoCode(String ISO_code){
        ISO_code = ISO_code.toUpperCase();
        对于(国家/地区:m 个国家/地区){
            String[] strArr = Country.ISO_code.split("/| ");
            for (字符串 s:strArr){
                if (ISO_code.equals(s))
                    返回国家;
            }
        }
        返回空值;
    }
    
    
    
    公共字符串[] getCountryNamesList(){
        String[] res = new String [mCountries.size()];
        整数i=0;
        对于(国家c:m国家){
            res[i] = c.Name;
            我++;
        }
        返回资源;
    }
    
    
    
    公共无效 readCountryCodesFromExcelWorkbook()
    {
        上下文上下文 = GlobalData.getInstance().getApp();
        工作簿mWorkbook;
        InputStream myRawResource = context.getResources().openRawResource(R.raw.countrycode_org);
        如果(myRawResource == null)
            Toast.makeText(context,"未找到 XML 文件",Toast.LENGTH_LONG).show();
        别的
            尝试 {
                WorkbookSettings ws = new WorkbookSettings();
                ws.setEncoding("Cp1252");
    
                mWorkbook = Workbook.getWorkbook(myRawResource);
                    //ArrayList; currentSheet = new ArrayList();
                    Sheetsheet = mWorkbook.getSheet(0);
    
                    int rowsNum =sheet.getRows();
                    for (int rowNum = 1; rowNum < rowsNum; rowNum++) {
                        //Log.d("行号", ""+行号);
                        int colsNum =sheet.getColumns();
                        String[] strArr = new String[colsNum];
                        布尔行IsFull = true;
                        for (int colNum = 0; colNum < colsNum; colNum++) {
                            strArr[colNum] = Sheet.getCell(colNum, rowNum).getContents();
                            if (strArr[colNum].length() == 0)
                                行已满 = false;
                        }
                        if (行已满)
                            addCountry(strArr[0],strArr[1],strArr[2]);
                    }
    
    
            } catch (BiffException e) {
                Toast.makeText(context,"读取 xml 文件时出错:BiffException",Toast.LENGTH_LONG).show();
                e.printStackTrace();
                返回 ;
            } catch (IOException e) {
                Toast.makeText(context,"读取 xml 文件时出错: IOException",Toast.LENGTH_LONG).show();
                e.printStackTrace();
                返回 ;
            }
    }
    
    
    公共国家[] getCountries(){
        返回 mCountries.toArray(new Country[0]);
    }
    
    
    
    公共课国家{
        公共字符串名称;
        公共字符串代码;
        公共字符串 ISO_code;
    
    }
    

    }

Had the same problem. Eventually I put all the data in excel and read the excel sheet.
Here is the implementation:

  1. copy-past the country code table from http://countrycode.org/ to Microsoft Excel file.
  2. Save the Excel file as 97-2003 compatible (.xls) in \res\raw\countrycode_org.xls
  3. Download JExcelApi from here
  4. Use the following class to read the file:

    public class CountryCodes {
    private HashMap mCountryByName = new HashMap();
    private HashMap mCountryByCode = new HashMap();;
    private 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;
    
    }
    

    }

无声情话 2024-11-23 17:12:02

第 1 步
您可以在以下网址中获取国家/地区呼叫代码及其ISO名称
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 格式。请参阅Java 中的开源 HTML 解析器

步骤 4通过电话号码您可以获得呼叫代码。例如,如果号码为“1-319-491-6338”,则呼叫代码为 1

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

Step-1
You can get country calling code as well as its ISO name in the following URL
http://en.wikipedia.org/wiki/List_of_country_calling_codes

or

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

Step-2 You can get page source of that file using java program. You will get file in HTMl format

Step-3 you can convert those HTML files into XML format using any of available parsers. see Open Source HTML Parsers in Java

Step-4 Form the phone number you can get the calling code. Example if the number is "1-319-491-6338" then calling code is 1

Step-5 Match this calling code against the calling code and country name list that you have got from XML parser. In this way you can get iso country

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文